
12 Faceting
Faceting is a technique that partitions a data set into subsets and displays each subset in its own panel. Also called trellising—a term borrowed from the lattice structures used by gardeners to grow climbing plants—faceting produces so-called “small multiples”: a series of smaller plots splitting the information into bite-sized chunks compared to a single-panel plot with grouped data. This approach is especially valuable when plotting all groups together would result in cluttered or hard-to-interpret graphics. You already saw an example of faceting in Section 6.2.2.1. In this chapter, we examine faceting in greater depth. First, we demonstrate faceting by a single grouping variable using ggplot2’s facet_wrap()
function (Section 12.1). Then, we explore faceting by two grouping variables with facet_grid()
.
12.1 Faceting by a Single Grouping Variable
Pygoscelis penguins, also known as brush-tailed penguins, are a genus found in the Antarctic and subantarctic regions. They are characterized by their black and white plumage and a unique swimming technique called porpoising, where they leap out of the water in a series of rapid jumps to breathe air. This genus includes three species: the Adélie penguin (Pygoscelis adeliae), the gentoo penguin (Pygoscelis papua), and the chinstrap penguin (Pygoscelis antarcticus). Researchers at Palmer Station in Antarctica have collected data on the bill length of these penguins (Gorman, Williams and Fraser, 2014). You will use these data, available from the palmerpenguins
package, to explore the differences between the species using various plot types.

library(tidyverse)
library(palmerpenguins)
penguins2 <-
penguins |>
drop_na(sex)
gg_penguins <-
ggplot(penguins2, aes(bill_length_mm, body_mass_g, color = species)) +
geom_point(alpha = 0.5) +
labs(
x = "Bill Length (mm)",
y = "Body Mass (g)",
color = "Species"
)
gg_penguins
gg_penguins +
facet_wrap(vars(species)) +
guides(color = "none")
Chunk 2:
overall_mean <- penguins2 |>
summarize(
bill_length_mm = mean(bill_length_mm),
body_mass_g = mean(body_mass_g)
)
ggplot(penguins2, aes(bill_length_mm, body_mass_g)) +
geom_point(aes(color = species), alpha = 0.5) +
geom_point(
aes(shape = "overall mean"),
data = overall_mean,
color = "black",
size = 3,
) +
scale_shape_manual(
values = c("overall mean" = 15), # square
labels = ~ str_to_sentence(.)
) +
guides(color = "none") +
labs(
x = "Bill Length (mm)",
y = "Body Mass (g)",
shape = NULL
) +
facet_wrap(vars(species))
12.2 Faceting by Two Grouping Variables
Chunk 3:
group_means <- penguins2 |>
summarize(
bill_length_mm = mean(bill_length_mm),
body_mass_g = mean(body_mass_g),
.by = c(sex, species)
)
ggplot(penguins2, aes(bill_length_mm, body_mass_g)) +
geom_point(aes(shape = sex, color = species, fill = species),
alpha = 0.5
) +
facet_grid(rows = vars(sex), cols = vars(species)) +
geom_point(
aes(shape = sex, fill = species),
data = group_means,
color = "black",
size = 3
) +
geom_point(
aes(shape = "overall mean"),
data = overall_mean,
color = "black",
size = 3
) +
scale_shape_manual(
values = c(
"female" = 25, # lower triangle
"male" = 24, # upper triangle
"overall mean" = 15 # square
),
labels = ~ str_to_sentence(.) # capitalize labels
) +
guides(
color = guide_legend(override.aes = list(size = 4, alpha = 1)),
shape = guide_legend(override.aes = list(size = 4)),
fill = "none" # fill is identical to color, so drop that legend
) +
labs(
x = "Bill Length (mm)",
y = "Body Mass (g)",
color = "Species",
shape = "Average"
)
12.3 Conclusion
Disadvantsate