16.5 Controlling space

facet_grid() has an additional parameter: space. This takes the same values as scales, but when space is “free”, each column (or row) will have width (or height) proportional to the range of the scale for that column (or row).

This is most useful for categorical scales, where we can assign space proportionally based on the number of levels in each facet.

mpg2$model <- reorder(mpg2$model, mpg2$cty)

mpg2$manufacturer <- reorder(mpg2$manufacturer, -mpg2$cty)

ggplot(mpg2, aes(cty, model)) + 
  geom_point() + 
  facet_grid(manufacturer ~ .) +
  theme(strip.text.y = element_text(angle = 0)) +
  labs(title = "fixed space")

ggplot(mpg2, aes(cty, model)) + 
  geom_point() + 
  facet_grid(manufacturer ~ ., space = "free") +
  theme(strip.text.y = element_text(angle = 0)) +
  labs(title = "free space only")

ggplot(mpg2, aes(cty, model)) + 
  geom_point() + 
  facet_grid(manufacturer ~ ., scales = "free") +
  theme(strip.text.y = element_text(angle = 0)) +
  labs(title = "free scales only")

ggplot(mpg2, aes(cty, model)) + 
  geom_point() + 
  facet_grid(manufacturer ~ ., scales = "free", space = "free") +
  theme(strip.text.y = element_text(angle = 0)) +
  labs(title = "free scales and space")