56 * 5.8[1] 324.8
38:494 - Limnology: The science of inland waters - 2024
ggplot2 package
steffilazerte
@steffilazerte@fosstodon.org
@steffilazerte
steffilazerte.ca
Compiled: 2024-09-25
ggplot2patchworkTaken this or a similar workshop before?
During activities consider…
- Extra activities labeled “Too Easy?”
- Using your own data
- Exploring other aspects of ggplot2 that interest you
Feel free to ask questions even if it’s not the “official” activity!
A programming language is a way to give instructions in order to get a computer to do something
R, what is 56 times 5.8?

(I made these slides with a mix of R and Quarto)



David Whittaker
Moral of the story?
Make friends, code in groups, learn together and don’t beat yourself up

Artwork by @allison_horst



functions() - Do things, Return thingsmean(), read_csv(), ggplot(), c(), etc.()mean(x = c(2, 10, 45)),mean(x = c(NA, 10, 2, 65), na.rm = TRUE)DataGenerally kept in vectors or data.frames
<- to assign values to objects (assignment)Ctrl-Enter# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
First, let’s look at the components of code, we’ll discuss why this works later
# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Packagesggplot2 and palmerpenguins
# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Functionslibrary(), ggplot(), aes(), geom_point()
# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
+
(Specific to ggplot)
# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Figure!
# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning
# First load the packages
library(palmerpenguins)
library(ggplot2)
# Now create the figure
ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Comments
Let’s get started


Artwork by @allison_horst
# A tibble: 344 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
<fct> <fct> <dbl> <dbl> <int> <int> <fct> <int>
1 Adelie Torgersen 39.1 18.7 181 3750 male 2007
2 Adelie Torgersen 39.5 17.4 186 3800 female 2007
3 Adelie Torgersen 40.3 18 195 3250 female 2007
4 Adelie Torgersen NA NA NA NA <NA> 2007
5 Adelie Torgersen 36.7 19.3 193 3450 female 2007
6 Adelie Torgersen 39.3 20.6 190 3650 male 2007
7 Adelie Torgersen 38.9 17.8 181 3625 female 2007
8 Adelie Torgersen 39.2 19.6 195 4675 male 2007
9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007
10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007
# ℹ 334 more rows

Artwork by @allison_horst

Your turn!
Run this code and look at the output in the console
library(palmerpenguins)palmerguins packagepenguins data

library(ggplot2)ggplot2 packageggplot() function
aes() and geom_point() etc.)

ggplot()data = Datasetaes = Aesthetics (how the data are used)
geom_point()geom function to display the dataggplot() call with +ggplots are essentially layered objects, starting with a call to
ggplot()

(Plot types)
Note: We only need 1 aesthetic here
ggplot count your datacolour = sex
ggplot automatically populates the legends (combining where it can)facet_wrap()Split plots by one grouping variable
facet_grid()Split plots by two grouping variables
Hint:
colouris for outlining with a colour,fillis for ‘filling’ with a colour
Too Easy? Split boxplots by sex and island
geom_line() is connect-the-dots, not a trend or linear modelNot what we’re looking for
Start with basic plot:
stat_smooth()lm is for “linear model” (i.e. trendline)stat_smooth()se = FALSEcolour to specify species)stat_smooth() automatically uses the same groupingToo Easy? Create a separate plot for each sex as well
Practice for later: Add proper labels to some of your previous plots
scale_ + (x or y) + type (continuous, discrete, date, datetime)
scale_x_continuous()scale_y_discrete()scale_ + aesthetic (colour, fill, size, etc.) + type (manual, continuous, datetime, etc.)
Or be very explicit:
viridis_d for “discrete” data
viridis_c for “continuous” data
Remove the association between a variable and an aesthetic
Note: When forcing, aesthetic is not inside
aes()
patchworkpatchworklibrary(patchwork)
g1 <- ggplot(data = penguins, aes(x = bill_length_mm, y = bill_depth_mm, colour = species)) +
geom_point()
g2 <- ggplot(data = penguins, aes(x = species, y = flipper_length_mm)) +
geom_boxplot()
g3 <- ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g, colour = species)) +
geom_point()patchworkpatchworkpatchworkpatchworkpatchworkpatchworkpatchworkpatchworkDemo
ggplot2, the function is just ggplot()+ at the end of the line?theme()’s, make sure you put these lines after bundled themes like theme_bw(), or they will be overwrittenfactor()ggplot(data = penguins, aes(x = factor(year), y = body_mass_g))patchwork websiteThank you!

steffilazerte.ca
sel@steffilazerte.ca
Slides created with Quarto on 2024-09-25
scales_....)😁