01 / 20

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.

R Programming

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.

One Crowded Plot Hard to read! Group A Group B Group C ✓ Clear and comparable!

Why Faceting Matters

Reduces Overplotting
Too many groups on one plot creates clutter. Faceting gives each group its own space.
Enables Comparison
Shared axes across panels let you spot differences in trends, spread, and outliers instantly.
Tells a Story
Faceted plots reveal how a relationship changes across categories: by region, year, species, etc.

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.

ggplot(data, aes(x, y)) + geom_point() + facet_wrap(~ variable) # split by one variable

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.

ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(~ class, ncol = 3)
compact midsize suv pickup 2seater minivan

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} \).

ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(drv ~ cyl)
4 cyl 6 cyl 8 cyl 4wd fwd rwd empty empty

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."

1
facet_wrap(~ var): split by a single variable, wrapping panels into a flexible grid.
2
facet_grid(row_var ~ col_var): rows defined by one variable, columns by another.
3
facet_grid(. ~ var): use a dot . to facet by only columns (or only rows with var ~ .).
# One variable, wrapped facet_wrap(~ species) # Two variables, structured grid facet_grid(sex ~ species) # One variable in columns only facet_grid(. ~ species)

Step-by-Step Faceting Workflow

1
Identify your grouping variable(s). Which categorical column(s) should define the panels? (e.g., species, year, region)
2
Build the base plot. Write your ggplot() + geom_*() code without faceting first. Confirm it works.
3
Choose wrap or grid. One variable → facet_wrap(). Two variables → facet_grid().
4
Add the facet layer. Append + facet_wrap(~ var) or + facet_grid(row ~ col).
5
Adjust layout. Control columns with ncol/nrow. Optionally free scales with scales = "free".

Example 1: Wrapping by Species

Using the iris dataset, let's plot Sepal.Length vs Sepal.Width faceted by Species.

library(ggplot2) ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point(color = "#818cf8") + facet_wrap(~ Species, ncol = 3) + labs(title = "Sepal Dimensions by Species")
setosa versicolor virginica

Example 2: Controlling Layout

Use ncol and nrow to control panel arrangement in facet_wrap(). Use scales = "free" to let axes vary per panel.

# Force 2 columns facet_wrap(~ Species, ncol = 2) # Free y-axis per panel facet_wrap(~ Species, scales = "free_y") # Free both axes facet_wrap(~ Species, scales = "free")
scales = "fixed"
Default: all panels share the same x and y ranges. Best for direct comparison.
scales = "free"
Each panel has its own axis range. Best when groups have very different scales.

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.

ggplot(mpg, aes(displ, hwy)) + geom_point(color = "#a78bfa", alpha = 0.7) + geom_smooth(method = "lm", se = FALSE, color = "#4ade80") + facet_grid(drv ~ cyl) + labs(title = "MPG by Drive Type and Cylinders")

The scales Parameter

"fixed"
All panels share the same x and y axis range. Default behavior.
"free_x"
Each panel gets its own x-axis range; y stays shared.
"free_y"
Each panel gets its own y-axis range; x stays shared.
"free"
Both axes vary independently per panel.

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.

# Show "variable: value" in strip facet_wrap(~ drv, labeller = label_both) # Custom labels via named vector drv_labels <- c("4" = "4-Wheel", "f" = "Front", "r" = "Rear") facet_wrap(~ drv, labeller = labeller(drv = drv_labels))

Common Mistakes

✕ Forgetting the Tilde
Writing facet_wrap(Species) instead of facet_wrap(~ Species). The tilde is required to create the formula.
✕ Faceting by Continuous
Using a numeric column like hwy creates one panel per unique value: dozens of nearly empty panels. Always facet by categorical variables.
✕ Too Many Panels
Faceting by a variable with 20+ levels creates an unreadable grid. Filter to the most important levels first, or group rare values.

Question 1 of 5

Which function would you use to create a \( \text{row} \times \text{column} \) grid of panels using two faceting variables?

Question 2 of 5

What does the tilde (\( \sim \)) symbol represent in facet_wrap(~ Species)?

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?

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?