Faceting in R
Small Multiples
Learn to split one plot into many panels using facet_wrap() and facet_grid() in ggplot2 for clearer data exploration.
What Are Small Multiples?
Small multiples are a series of similar charts arranged in a grid, each showing a different subset of the same dataset. Instead of cramming all groups into one crowded plot, each panel shows one group with identical axes and scales.
Why Faceting Matters
Faceting in ggplot2
In ggplot2, faceting is added as a layer to any plot. You build your base plot with ggplot() + geom_*() and then append a facet function to split it into panels.
facet_wrap(): Wrapping Panels
facet_wrap() takes a single categorical variable and creates one panel per level. Panels are laid out left to right, wrapping to a new row when the row fills up.
facet_grid(): Row \( \times \) Column Grid
facet_grid() maps two variables to rows and columns, creating a structured matrix of panels. The syntax is \( \text{rows} \sim \text{cols} \).
facet_wrap vs facet_grid
| facet_wrap() | facet_grid() | |
|---|---|---|
| Variables | Typically 1 | 1 or 2 (\( \text{rows} \sim \text{cols} \)) |
| Layout | Wraps into rows | Fixed \( \text{row} \times \text{column} \) matrix |
| Syntax | \( \sim \text{var} \) | \( \text{row\_var} \sim \text{col\_var} \) |
| Control | ncol, nrow | Automatic from variable levels |
| Empty Panels | None (skipped) | Yes (shows all combos) |
| Best For | One categorical variable | Exploring two-way interactions |
The Formula Syntax
Both faceting functions use R's formula notation with the tilde \( \sim \) operator. The tilde means "modeled by" or "split by."
facet_wrap(~ var): split by a single variable, wrapping panels into a flexible grid.facet_grid(row_var ~ col_var): rows defined by one variable, columns by another.facet_grid(. ~ var): use a dot . to facet by only columns (or only rows with var ~ .).Step-by-Step Faceting Workflow
+ facet_wrap(~ var) or + facet_grid(row ~ col).scales = "free".Example 1: Wrapping by Species
Using the iris dataset, let's plot Sepal.Length vs Sepal.Width faceted by Species.
Example 2: Controlling Layout
Use ncol and nrow to control panel arrangement in facet_wrap(). Use scales = "free" to let axes vary per panel.
Example 3: Two-Variable Grid
With the mpg dataset, facet by drv (drive type: rows) and cyl (cylinders: columns) to see how engine size and highway MPG relate across car categories.
The scales Parameter
Customizing Panel Labels
By default, panel strips show the value of the faceting variable. You can customize this with the labeller argument or by renaming factor levels.
Common Mistakes
facet_wrap(Species) instead of facet_wrap(~ Species). The tilde is required to create the formula.Question 1 of 5
Which function would you use to create a \( \text{row} \times \text{column} \) grid of panels using two faceting variables?
facet_grid(row_var ~ col_var) creates a structured matrix with one variable defining rows and another defining columns. facet_wrap() wraps panels sequentially and does not create a true \( \text{row} \times \text{column} \) grid.Question 2 of 5
What does the tilde (\( \sim \)) symbol represent in facet_wrap(~ Species)?
lm(y ~ x).Question 3 of 5
You want each panel to have its own y-axis range while keeping the x-axis the same. Which argument do you add?
scales = "free_y" allows each panel to set its own y-axis range while keeping the x-axis shared. "free" would free both axes, and "free_x" would only free the x-axis.Question 4 of 5
What is the key difference between facet_wrap() and facet_grid() regarding empty combinations of factor levels?
Question 5 of 5
A student writes: facet_grid(. ~ cyl). What layout will this produce?
facet_grid(. ~ cyl), the dot . means "nothing" on the row side. So the result is a single row of panels, one per level of cyl (columns). This is different from facet_wrap(), which would wrap panels into multiple rows if there are many levels.