Chapter 11. Facets
One of the most useful techniques in data visualization is rendering
groups of data alongside each other, making it easy to compare the
groups. With ggplot2, one way to do this is by mapping a discrete
variable to an aesthetic, like x
shape. Another way of doing this is to create a subplot for each group and draw
the subplots side by side.
These kinds of plots are known as Trellis displays. They’re implemented in the lattice package as well as in the ggplot2 package. In ggplot2, they’re called facets. In this chapter I’ll explain how to use them.
11.1 Splitting Data into Subplots with Facets
You want to plot subsets of your data in separate panels.
facet_wrap(), and specify the variables on which
facet_grid(), you can specify a variable to split the data into
vertical subpanels, and another variable to split it into horizontal
subpanels (Figure 11-1):
# Create the base plot
# Faceted by drv, in vertically arranged subpanels
# Faceted by cyl, in horizontally arranged subpanels
# Split by drv (vertical) and cyl (horizontal)