Chapter 15 Facets

An amazing features of ggplot, is facetting. Rather than explaining what they are, I will just show you:

ggplot(mpg, aes(x=cty)) + geom_histogram(binwidth=1) + facet_wrap(~manufacturer)

Facetting groups the data and displays the data for each group. This allows for comparing multiple groups readily. Let’s see some other examples. Remember this graph:

ggplot(mpg, aes(x=displ, y=cty, colour=drv)) + 
  geom_point() +
  geom_smooth(method="lm", se=FALSE)

With facets:

ggplot(mpg, aes(x=displ, y=cty)) + 
  geom_point() +
  geom_smooth(method="lm", se=FALSE) +
  facet_wrap(~drv)

We can also put them under one another:

ggplot(mpg, aes(x=displ, y=cty)) + 
  geom_point() +
  geom_smooth(method="lm", se=FALSE) +
  facet_wrap(~drv, nrow=3)

Or use multiple variables with facet_grid

ggplot(mpg, aes(x=displ, y=cty)) + 
  geom_point() +
  geom_smooth(method="lm", se=FALSE) +
  facet_grid(year~drv)