[1] 6
[1] 1
[1] 16
I started Vet school in 2010 with the intention of becoming a wildlife vet
•
Got a bit derailed and seduced by the idea of becoming an epidemiologist
•
Ended up in academia and since 2016 I have been working with programming and modeling of infectious diseases
About you: https://forms.gle/b6P8XhiEstn3sJne9
What it is | What is NOT |
---|---|
Introduction to data visualization | Introduction to R programming |
Operators are characters with a specific function in R for example
[1] 6
[1] 1
[1] 16
Later we will see other kind of operators, but… DONT STRESS about learning everything.
Objects in R are containers for information, we can create objects with any names we want that start with a letter
Using the c()
function
[1] TRUE
[1] FALSE
[1] TRUE
Notice that we are using operators to make the comparisons
Functions are a special kind of object. Functions are objects that require arguments, the arguments needs to be inside parentheses.
# create a sequence of numbers
seq(
from = 0, # Starting number
to = 80, # Ending number
by = 20 # number increment of the sequence
)
[1] 0 20 40 60 80
Notice that the arguments are named in the function, the arguments in the function seq()
function are from
, to
, by
.
We can create our own functions, which we will talk more about in the labs
x <- seq(from = 5, to = 23, length.out = 10) # create a sequence of numbers
y <- seq(from = 0.1, to = 0.78, length.out = 10) # Create another sequence
mean(x*y) # Get the mean of the multiplication
[1] 7.406667
Objects:
- x
- y
Operators:
- *
- <-
- =
Functions:
- seq()
- mean()
Arguments:
- from
- to
- lengt.out
Using the ?
operator:
! WARNING: CHAT GPT CAN GIVE INCORRECT INFORMATION !
R is like a calculator, we can make mathematical operations, for example:
You can store more than one value using vectors, to create a vector of numbers we use c()
.
x <- c(5, 6, 7, 8, 9, 10) # create a sequence form 5 to 10
y = 5:10 # create the same sequence but with a different approach
x == y # ask R if the objects have the same information
[1] TRUE TRUE TRUE TRUE TRUE TRUE
Using the keys “alt” + “-” will automatically add the operator <-
.
When we have a vector, we can ask R specific values inside an object by using the operator [ ]
and specifying which ones we want.
When we have a vector, we can ask R specific values inside an object by using the operator [ ]
and specifying which ones we want.
We can put functions inside function, for example, to get \(\sqrt{\sum_1^n x}\) the square root of a sum of the numbers in x we can use:
The following function has only one argument which is a name (string) and just pastes some text before and after:
\[\sqrt{\sum_1^n x}\]
%>%
Pipes (%>%
), can connect several functions to an object.
For example, if we want to execute a function F1()
followed by another function F2()
for the object x
:
\[\sqrt{\sum_1^n x}\]
Instead of this:
Instead of this:
Instead of this:
# Get the number of outgoing and incoming shipments
Out <- rename(summarise(group_by(mov, id_orig), Outgoing = n()), id = id_orig)
We can write this:
# Get the number of outgoing and incoming shipments
Out <- mov %>% # This is the movement data set
group_by(id_orig) %>% # Group by origin
summarise(Outgoing = n()) %>% # Count the number of observations
rename(id = id_orig) # Rename the variable
And we can break down the code easier!
Instead of the %>%
, in ggplot we connect pieces of code with +
The basic components that we need to define for a plot are the following:
municipality | location | Loc | date | year | captures | treated | lat | lon | trap_type |
---|---|---|---|---|---|---|---|---|---|
Temascaltepec | San Pedro Tenayac | Cueva el Uno | 11/06/14 | 2014 | 6 | 6 | 18.03546 | -100.2095 | 1 |
Tlatlaya | Nuevo Copaltepec | La alcantarilla | 12/05/05 | 2005 | 3 | 2 | 18.40417 | -100.2688 | 1 |
Tlatlaya | Nuevo Copaltepec | La alcantarilla | 12/05/07 | 2007 | 30 | 29 | 18.40417 | -100.2688 | 4 |
Tlatlaya | Nuevo Copaltepec | La alcantarilla | 12/03/09 | 2009 | 0 | 0 | 18.40417 | -100.2688 | 3 |
Tlatlaya | Nuevo Copaltepec | La alcantarilla | 10/08/10 | 2010 | 4 | 3 | 18.40417 | -100.2688 | 1 |
year | n |
---|---|
2005 | 167 |
2006 | 103 |
2007 | 249 |
2008 | 143 |
2009 | 125 |
Introduction to ggplot2
Comments
COMMENT AS MUCH AS POSSIBLE!
What is the difference between line 1 and 2?
YES! the
#
character will make everything after it a comment in that line of code