Marine Ecosystem Dynamics
tidyversemagrittrtidyrdplyrggplot2
tidyverse is a collection of packages%>%magrittr%>% takes the data from the left and place it to the right
x %>% function() = function(x)Without the pipe operator:
tidyrA table is tidy if:
Key functions:
pivot_longerpivot_wideruniteseparate
tidyr - iris example| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | id |
|---|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa | 2 |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa | 3 |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa | 4 |
tidyr - pivot_longer| Species | id | Parameter | Size |
|---|---|---|---|
| setosa | 1 | Sepal.Length | 5.1 |
| setosa | 1 | Sepal.Width | 3.5 |
| setosa | 1 | Petal.Length | 1.4 |
| setosa | 1 | Petal.Width | 0.2 |
tidyr - separate| Species | id | Organ | Measure | Size |
|---|---|---|---|---|
| setosa | 1 | Sepal | Length | 5.1 |
| setosa | 1 | Sepal | Width | 3.5 |
| setosa | 1 | Petal | Length | 1.4 |
| setosa | 1 | Petal | Width | 0.2 |
tidyr - pivot_wider| Species | id | Measure | Sepal | Petal |
|---|---|---|---|---|
| setosa | 1 | Length | 5.1 | 1.4 |
| setosa | 1 | Width | 3.5 | 0.2 |
| setosa | 2 | Length | 4.9 | 1.4 |
| setosa | 2 | Width | 3.0 | 0.2 |
tidyr - unite| Species | id | Measure | Sepal/Petal |
|---|---|---|---|
| setosa | 1 | Length | 5.1/1.4 |
| setosa | 1 | Width | 3.5/0.2 |
| setosa | 2 | Length | 4.9/1.4 |
| setosa | 2 | Width | 3/0.2 |
dyplrdplyr simplifies the data manipulation with self-explanatory functions:
filter observations based on their valuesmutate a new column as a function of othersselect variables based on their namesgroup_by variablesummarise the datairis %>%
dplyr::filter(Petal.Length >= 1.4) %>%
dplyr::mutate(Sepal_Ratio = Sepal.Length/Sepal.Width) %>%
dplyr::select(Species, Sepal_Ratio) %>% # equaivalent to select(-c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))
dplyr::group_by(Species) %>%
dplyr::summarise(Average_ratio = mean(Sepal_Ratio),
standard_deviation = sd(Sepal_Ratio))It is very important to look at the data. Totally different data might have similar statistics…

| statistics | value |
|---|---|
| mean_x | 54.27 |
| mean_y | 47.83 |
| sd_x | 16.77 |
| sd_y | 26.94 |
ggplot2ggplot2
species_palette <- c("#F8766D", "#00BA38", "#619CFF")
size_vector <- iris$Petal.Width
plot(x = iris$Sepal.Length,
y = iris$Sepal.Width,
col = species_palette[iris$Species],
bg = species_palette[iris$Species],
pch = 21,
cex = size_vector,
xlim = c(min(iris$Sepal.Length), max(iris$Sepal.Length)),
ylim = c(min(iris$Sepal.Width), max(iris$Sepal.Width)),
xlab = "Sepal.Length",
ylab = "Sepal.Width")
legend("topright", legend = levels(iris$Species), col = species_palette, pch = 21, pt.bg = species_palette, cex = 1.2, title = "Species")
grid(lwd = 1, lty = "dotted")gg)ggplot2 - Dataggplot2 - Aestheticsggplot2 - Geometryggplot2 - Geometryggplot2 - Statisticsggplot2 - Facetsggplot2 - Coordinatesggplot2 - Themesggplot2 - Themesggplot(data = iris,
mapping = aes(x = Sepal.Length,
y = Sepal.Width)) +
geom_point(mapping = aes(col = Species)) +
stat_smooth(method = "lm") +
stat_smooth(method = "lm",
mapping = aes(col = Species,
fill = Species)) +
facet_wrap(~Species) +
coord_polar() +
theme_light() +
theme(axis.line = element_line(color = "red"))
ggplot2 - Themesggplot(data = iris,
mapping = aes(x = Sepal.Length,
y = Sepal.Width)) +
geom_point(mapping = aes(col = Species)) +
stat_smooth(method = "lm") +
stat_smooth(method = "lm",
mapping = aes(col = Species,
fill = Species)) +
facet_wrap(~Species) +
coord_polar() +
theme_light() +
theme(axis.line = element_line(color = "red"),
strip.text = element_text(size = 13, color = "pink"))
ggplot2 - Extra tipsggplot(data = iris,
mapping = aes(x = Sepal.Length,
y = Sepal.Width)) +
geom_point(mapping = aes(col = Species)) +
stat_smooth(method = "lm") +
stat_smooth(method = "lm",
mapping = aes(col = Species,
fill = Species)) +
facet_wrap(~Species) +
coord_polar() +
theme_light() +
theme(axis.line = element_line(color = "red"),
strip.text = element_text(size = 13, color = "pink")) +
labs(title = "Sepal", x = "Length" , y = "Width", subtitle = "relationship between width\nand length", caption = "Caption appear here")
ggplot2 - Extra tipsggplot(data = iris,
mapping = aes(x = Sepal.Length,
y = Sepal.Width)) +
geom_point(mapping = aes(col = Species)) +
stat_smooth(method = "lm") +
stat_smooth(method = "lm",
mapping = aes(col = Species,
fill = Species)) +
facet_wrap(~Species) +
coord_polar() +
theme_light() +
theme(axis.line = element_line(color = "red"),
strip.text = element_text(size = 13, color = "pink")) +
labs(title = "Sepal", x = "Length" , y = "Width", subtitle = "relationship between width\nand length", caption = "Caption appear here") +
scale_color_manual(values = c("forestgreen", "firebrick", "burlywood"))
