11.1 Splitting Data into Subplots with Facets

11.1.1 Problem

You want to plot subsets of your data in separate panels.

11.1.2 Solution

Use facet_grid() or facet_wrap(), and specify the variables on which to split.

With 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):

Faceting horizontally by drv (top); Faceting vertically by cyl (middle); Faceting in both directions, with both variables (bottom)Faceting horizontally by drv (top); Faceting vertically by cyl (middle); Faceting in both directions, with both variables (bottom)Faceting horizontally by drv (top); Faceting vertically by cyl (middle); Faceting in both directions, with both variables (bottom)

Figure 11.1: Faceting horizontally by drv (top); Faceting vertically by cyl (middle); Faceting in both directions, with both variables (bottom)

With facet_wrap(), the subplots are laid out horizontally and wrap around, like words on a page, as in Figure 11.2:

A scatter plot with facet_wrap() on class

Figure 11.2: A scatter plot with facet_wrap() on class

11.1.3 Discussion

With facet_wrap(), the default is to use the same number of rows and columns. In Figure 11.2, there were seven facets, and they fit into a 3×3 square. To change this, you can pass a value for nrow or ncol:

The choice of faceting direction depends on the kind of comparison you would like to encourage. For example, if you want to compare heights of bars, it’s useful to make the facets go horizontally. If, on the other hand, you want to compare the horizontal distribution of histograms, it makes sense to make the facets go vertically.

Sometimes both kinds of comparisons are important – there may not be a clear answer as to which faceting direction is best. It may turn out that displaying the groups in a single plot by mapping the grouping variable to an aesthetic like color works better than using facets. In these situations, you’ll have to rely on your judgment.