Multiple graphs on one page (ggplot2)

Problem

You want to put multiple graphs on one page.

Solution

The easy way is to use the multiplot function, defined at the bottom of this page. If it isn’t suitable for your needs, you can copy and modify it.

First, set up the plots and store them, but don’t render them yet. The details of these plots aren’t important; all you need to do is store the plot objects in variables.

  1. library(ggplot2)
  2. # This example uses the ChickWeight dataset, which comes with ggplot2
  3. # First plot
  4. p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
  5. geom_line() +
  6. ggtitle("Growth curve for individual chicks")
  7. # Second plot
  8. p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
  9. geom_point(alpha=.3) +
  10. geom_smooth(alpha=.2, size=1) +
  11. ggtitle("Fitted growth curve per diet")
  12. # Third plot
  13. p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
  14. geom_density() +
  15. ggtitle("Final weight, by diet")
  16. # Fourth plot
  17. p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
  18. geom_histogram(colour="black", binwidth=50) +
  19. facet_grid(Diet ~ .) +
  20. ggtitle("Final weight, by diet") +
  21. theme(legend.position="none") # No legend (redundant in this graph)

Once the plot objects are set up, we can render them with multiplot. This will make two columns of graphs:

  1. multiplot(p1, p2, p3, p4, cols=2)
  2. #> `geom_smooth()` using method = 'loess'

plot of chunk unnamed-chunk-3

multiplot function

This is the definition of multiplot. It can take any number of plot objects as arguments, or if it can take a list of plot objects passed to plotlist.

  1. # Multiple plot function
  2. #
  3. # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
  4. # - cols: Number of columns in layout
  5. # - layout: A matrix specifying the layout. If present, 'cols' is ignored.
  6. #
  7. # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
  8. # then plot 1 will go in the upper left, 2 will go in the upper right, and
  9. # 3 will go all the way across the bottom.
  10. #
  11. multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  12. library(grid)
  13. # Make a list from the ... arguments and plotlist
  14. plots <- c(list(...), plotlist)
  15. numPlots = length(plots)
  16. # If layout is NULL, then use 'cols' to determine layout
  17. if (is.null(layout)) {
  18. # Make the panel
  19. # ncol: Number of columns of plots
  20. # nrow: Number of rows needed, calculated from # of cols
  21. layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
  22. ncol = cols, nrow = ceiling(numPlots/cols))
  23. }
  24. if (numPlots==1) {
  25. print(plots[[1]])
  26. } else {
  27. # Set up the page
  28. grid.newpage()
  29. pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
  30. # Make each plot, in the correct location
  31. for (i in 1:numPlots) {
  32. # Get the i,j matrix positions of the regions that contain this subplot
  33. matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
  34. print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
  35. layout.pos.col = matchidx$col))
  36. }
  37. }
  38. }