Display multiple plots in one image with the different facet
functions. An advantage of this method is that all axes share the same scale across charts, making it easy to compare them at a glance. We'll use the mpg
dataset included in ggplot2
.
Wrap charts line by line (attempts to create a square layout):
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
facet_wrap(~class)
Display multiple charts on one row, multiple columns:
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(.~class)
Display multiple charts on one column, multiple rows:
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(class~.)
Display multiple charts in a grid by 2 variables:
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(trans~class) #"row" parameter, then "column" parameter