ggplot2 Facets: Create Multiple Panels with facet_wrap() and facet_grid()
Faceting splits one plot into a set of smaller plots based on categorical variables. Small multiples make group comparisons easier while preserving a common visual design.
This lesson is part of the ggplot2 tutorial. It uses the built-in mpg dataset included with ggplot2, so you can run the examples after loading the package.
Create panels with facet_wrap
facet_wrap() creates one panel for each level of a categorical variable. The same mapping and geom are repeated in each panel.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ class)
Choose the panel layout
Use nrow or ncol to control the number of panel rows or columns. The layout should fit the number of categories and the available page width.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ class, ncol = 3)
Create a two-way grid
facet_grid(rows ~ columns) creates panels for combinations of two variables. Use a dot to leave one dimension unrestricted.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_grid(drv ~ year)
Use free scales thoughtfully
scales = "free_x", "free_y" or "free" allows panel axes to vary. This can reveal patterns hidden by a shared scale, but readers can no longer compare positions as directly.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ class, scales = "free")
Add panel-specific context
Use labs() for overall labels and strip labels to identify each panel. Keep the same geoms and scales when the goal is direct comparison.
ggplot(mpg, aes(displ, hwy,
colour = drv)) +
geom_point(show.legend = FALSE) +
facet_wrap(~ class) +
labs(title = "Engine size and fuel economy",
x = "Displacement (litres)",
y = "Highway mpg")
Common mistakes and troubleshooting
- Could not find function: run
library(ggplot2)in the current R session. Install it first withinstall.packages("ggplot2")if needed. - Object not found: check spelling and confirm the column exists in the data frame. Use
names(mpg)to inspect column names. - Unexpected legend: a variable mapped inside
aes()creates a scale and usually a legend. A fixed setting belongs outsideaes(). - Plot does not appear: make sure the complete expression is evaluated. In a script or function, explicitly call
print(p)when displaying a stored ggplot object. - Warning about removed rows: inspect missing or out-of-range values. Do not suppress the warning until you understand why observations were removed.
Next step: Continue with the next lesson using the navigation at the bottom of this page, or return to the ggplot2 tutorial overview.
ggplot2 FAQ
What is facet_wrap() in ggplot2?
facet_wrap() wraps a one-dimensional sequence of panels into rows and columns, usually based on one faceting variable.
What is the difference between facet_wrap and facet_grid?
facet_wrap() lays out panels flexibly. facet_grid() creates a grid defined by row and column variables.
How do I allow different axes in facets?
Set scales = "free", "free_x" or "free_y" in the facet function. Free scales improve within-panel detail but make direct comparisons harder.