Splitting graphs into facets

Andrew Irwin, a.irwin@dal.ca

2024-02-01

Splitting a plot into facets

  • Using one categorical variable

  • Using two categorical variables

  • Using quantitative variables

One categorical variable

  • facet_wrap( ~ variable)
mtcars |> ggplot(aes(x=wt, y=mpg)) + geom_point()
mtcars |> ggplot(aes(x=wt, y=mpg)) + geom_point() +
  facet_wrap(~cyl)
penguins |>
  ggplot(aes(x=body_mass_g, y=flipper_length_mm, color=sex)) +
  geom_point() + 
  facet_wrap( ~ species)

Using two categorical variables

  • facet_grid( y ~ x)
  • facet_wrap( ~ x + y)
mtcars |> ggplot(aes(x =wt, y=mpg)) + geom_point() +
  facet_grid(gear~cyl)
mtcars |> ggplot(aes(x =wt, y=mpg)) + geom_point() +
  facet_grid(gear~cyl, 
             labeller = labeller(.cols = function(x) paste(x, "cylinders"),
                                 .rows = label_both))
penguins |>
  ggplot(aes(x=body_mass_g, y=flipper_length_mm, color=sex)) +
  geom_point() + 
  facet_grid(species ~ island)
penguins |>
  ggplot(aes(x=body_mass_g, y=flipper_length_mm, color=sex)) +
  geom_point() + 
  facet_wrap(~ species + island)
gapminder |>
  ggplot(aes(x=year, y = lifeExp, group = country)) + 
  geom_line() +
  facet_grid( continent ~ . )
gapminder |>
  ggplot(aes(x=year, y = lifeExp)) + 
  geom_line(aes(group = country), color = "gray40", linewidth=0.25) +
  stat_summary(fun.data = "mean_cl_normal", color = "blue") + 
  facet_grid( continent ~ . )

Splitting on a quantitative variable

  • Start by converting the quantitative variable into a categorical one

For example:

penguins |> 
  filter(!is.na(bill_length_mm)) |>
  mutate(bill_category = cut_number(bill_length_mm, n = 4)) |>
  select(species, bill_length_mm, bill_category, everything())
# A tibble: 342 × 9
   species bill_length_mm bill_category island   bill_depth_mm flipper_length_mm
   <fct>            <dbl> <fct>         <fct>            <dbl>             <int>
 1 Adelie            39.1 [32.1,39.2]   Torgers…          18.7               181
 2 Adelie            39.5 (39.2,44.5]   Torgers…          17.4               186
 3 Adelie            40.3 (39.2,44.5]   Torgers…          18                 195
 4 Adelie            36.7 [32.1,39.2]   Torgers…          19.3               193
 5 Adelie            39.3 (39.2,44.5]   Torgers…          20.6               190
 6 Adelie            38.9 [32.1,39.2]   Torgers…          17.8               181
 7 Adelie            39.2 [32.1,39.2]   Torgers…          19.6               195
 8 Adelie            34.1 [32.1,39.2]   Torgers…          18.1               193
 9 Adelie            42   (39.2,44.5]   Torgers…          20.2               190
10 Adelie            37.8 [32.1,39.2]   Torgers…          17.1               186
# ℹ 332 more rows
# ℹ 3 more variables: body_mass_g <int>, sex <fct>, year <int>
penguins |> filter(!is.na(bill_length_mm)) |>
  mutate(bill_category = cut_number(bill_length_mm, n = 4)) |>
  ggplot(aes(x= body_mass_g, y = flipper_length_mm, color = species)) + 
  geom_point() + 
  facet_wrap( ~ bill_category, label = labeller(.rows = label_both))

Summary

  • facet_wrap(), facet_grid()

  • You can set the number of rows and columns with nrow = Y or ncol=X

  • You can label facets with both the variable and value using labeller = labeller(.rows = label_both)

Further reading

  • More examples with different data in the course notes

  • R4DS Section 3.5

  • Wilke Multi-panel figures discussing facets and other suggestions for combining different views of data together into one figure

  • Healy Section 4.3

Task

Practice these skills by making the plots in the assigned Task.