Faceting in R

Learn the details of the faceting feature in R.

Advanced faceting

Up until now, we’ve faceted our plots in the qplot() function using the facets= argument. This has two potential drawbacks:

  1. Sometimes, we have many panes we’d like to create in a single dimension, so many that they would need to wrap around into multiple rows.
  2. While it can be convenient to have our plots all be on the same axes, sometimes we don’t want that.

For example, when we don’t use scales='free', the figure has a lot of unused space in the left panel. See the figure here.FigureFaceting

There are two functions for faceting outside of what’s built into the qplot() function. The first is facet_grid(), which does the same thing that we did before in qplot(). The other is facet_wrap(), which allows us to wrap our facets into multiple rows. The advantage of both of these functions is that we can include the scales='free' argument in them, which will let the axes vary independently for each panel.

For example, we can modify our code from earlier when we made linear regressions faceted by resource treatment. Now, instead of the faceting being coded inside the qplot() function, we code it separately in facet_grid(). We’ll see an example of the facet_wrap() function in use later.

Get hands-on with 1200+ tech skills courses.