Renders multiple ggplot plots in one image

multiplot(
  ...,
  plotlist = NULL,
  cols = 1,
  layout = NULL,
  heights = NULL,
  widths = NULL
)

Arguments

...

ggplot objects (or grobs)

plotlist

a list of ggplot objects

cols

number of columns in layout

layout

a matrix specifying the layout. if present, cols is ignored

heights

a unit vector giving the relative height of each row (optional)

widths

a unit vector giving the relative width of each row (optional)

Note

If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), then plot 1 will go in the upper left, 2 will go in the upper right, and 3 will go all the way across the bottom.

References

Adapted by Joseph Larmarange from Winston Chang, Cookbook for R, http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/

Examples

require(ggplot2)
p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point()
p2 <- ggplot(mtcars, aes(factor(cyl))) +
  geom_bar()
p3 <- ggplot(mtcars, aes(factor(cyl), mpg)) +
  geom_violin()
p4 <- ggplot(mtcars, aes(factor(cyl), mpg)) +
  geom_boxplot()

multiplot(p1, p2, p3, p4)

multiplot(p1, p2, p3, p4, cols = 2)

multiplot(p1, p2, p3, layout = matrix(c(1, 2, 3, 3), nrow = 2))

multiplot(p1, p2, p3, layout = matrix(c(1, 2, 3, 3), nrow = 2, byrow = TRUE))

multiplot(p1, p2, p3, layout = matrix(c(1, 2, 3, 3), nrow = 2, byrow = TRUE), heights = c(3, 1))