Multiplot

This is a brief bit on using the multiplot function with ggplot2.

First, you can create multiple plots from the same dataset using ggplot2. But not unrelated plots or plots where the data is stored in separate columns of the same dataset. See below.

The multiplot function:

First initialize the multiplot function and ggplot2 library.

library(ggplot2)
options(width=60)

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# 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.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}


Then draw up a few plots with different object names.

p1 <- ggplot(mtcars, aes(factor(cyl), mpg))
p1 <- p1 + geom_boxplot()

p2 <- qplot(rating, data=movies, weight=votes, geom="histogram")

p3 <- ggplot(mtcars, aes(x = mpg)) + geom_dotplot()

msamp <- movies[sample(nrow(movies), 1000), ]
p4 <- ggplot(msamp, aes(year, rating)) + geom_point()
p4 <- p4 + stat_quantile()

p5 <- ggplot(mtcars, aes(y=wt, x=mpg, colour=factor(cyl)))
p5 <- p5 + stat_smooth(method=lm, aes(fill = factor(cyl))) + geom_point()

p6 <- ggplot(mtcars, aes(wt, mpg))
p6 <- p6 + geom_point(aes(size = qsec))


Then specify the plots and number of columns using multiplot.

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



Error: No layers in plot

Notice that these plots are scripted like this:

p1 <- ggplot(mtcars, aes(factor(cyl), mpg))
p1 <- p1 + geom_boxplot()

I found this same plot on the ggplot2 website scripted like this:

p1 <- ggplot(mtcars, aes(factor(cyl), mpg))
p1 + geom_boxplot()

The absence of the assignment operator tells R to print the object p1.

This works for single plots in ggplot2 but the second line “p1 + geom_boxplot” must be changed to “p1 <- p1 + geom_boxplot()” if you use the multiplot function. If you don’t change it for multiplot you get the error: “Error: No layers in plot”



Multiple plots within a ggplot2 function.

This is how you can draw multiple plots within ggplot2. Notice that the three plots all share the same “cyl” variable from the mtcars dataset. See here for more info.

c <- ggplot(mtcars, aes(y=wt, x=mpg)) + facet_grid(. ~ cyl)
c + stat_smooth(method=lm) + geom_point()