Facets

  • Facets are a way of splitting a plot into multiple panels based on the values of a variable.

  • grid and wrapped

  • when to use this vs aesthetics?

ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  facet_wrap(~class)

Exercises

  • divides in to bins
ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_wrap(~ hwy)

ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_wrap(~ cyl)

ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_wrap(~ cyl)

  • no. cyclinders increase -> bigger engines, more fuel consumption
ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_wrap(~ class, nrow = 2, ncol = 4)

ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_wrap(~ class, nrow = 2)

  • The scales argument controls whether the axes in the facets are fixed or allowed to vary independently.

  • Options:

  • fixed (default): All facets share the same x and y-axis scales.

  • free_x: Each facet has its own x-axis scale.

  • free_y: Each facet has its own y-axis scale.

  • free: Each facet has its own x and y-axis scales.

  • use when scale are different/the same

ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_wrap(~ class, scales = "free")