In-Class Multi-Panel Plots Exercise
Intro to Quantitative Ecology:
We’ll use the palmerpenguins
data for this exercise.
It’s pretty straightforward to make simple multi-panel plot in R
using the par()
function.
To begin, I’ll create two simple plots form the penguins data, then combine them into a single figure.
I’ll make a scatterplot of penguin flipper length and body mass:
plot(
flipper_length_mm ~ body_mass_g, data = penguins,
main = "Mike's Beautiful\nScatterplot",
xlab = "Body Mass (g)",
ylab = "Flipper Length (mm)")
Next, I’ll make a conditional boxplot of body mass conditioned on species:
boxplot(
body_mass_g ~ species, data = penguins,
main = "Mike's Awesome\nConditional Boxplot",
xlab = "Penguin Species",
ylab = "Body Mass (g)")
par()
functionIt’s super easy to make gridded multi-panel plots.
You can simply use the function par()
with the
mfrow
argument.
The mfrow
argument expects a vector of two numbers:
I can combine my plots above into a 1-row, 2-column grid by inserting
a call to par()
before my plotting code:
# specify a grid with 1 row, 2 columns
par(mfrow = c(1, 2))
# First plot
plot(
flipper_length_mm ~ body_mass_g, data = penguins,
main = "Mike's Lovely\nScatterplot",
xlab = "Body Mass (g)",
ylab = "Flipper Length (mm)")
# Second plot
boxplot(
body_mass_g ~ species, data = penguins,
main = "Mike's Awesome\nConditional Boxplot",
xlab = "Penguin Species",
ylab = "Body Mass (g)")
How could you create a 2 by 2 grid of plots?
So far in the course, we’ve created histograms using
hist()
, simple and conditional boxplots using
boxplot()
, and scatterplots using plot()
. Now
we’ll learn how to combine several individual plots into a single
multi-panel figure.
palmerpenguins
package.
penguins
that you like.Submit a document that includes the following sections