How to Use the multiplot() Function in R


One of the most common data visualization libraries in the R programming language is ggplot2, which can be used to create a variety of customizable graphs and charts.

Often when using ggplot2, you may want to display multiple plots at once on the same page.

The easiest way to do so is by using the multiplot() function from the scater package.

This function uses the following basic syntax:

multiplot(p1, p2, p3, col=1, …)

where:

  • p1, p2, p3: The names of various plots created using ggplot2
  • col: The number of columns to use when displaying the plots

Note that if you leave out the col argument then the multiplot() function will simply use one column to display each of the charts in a vertical format.

The following example shows how to use this function in practice.

Example: How to Use the multiplot() Function in R

Suppose we would like to create multiple scatterplots using the built-in mtcars dataset in R and display each plot on the same page.

The following syntax shows how to do so:

#load necessary packages
library(ggplot2)
library(scater)

#create first plot
p1 <- ggplot(mtcars, aes(x=disp, y=qsec)) +
   geom_point() +
   ggtitle("disp vs. qsec")

#create second plot
p2 <- ggplot(mtcars, aes(x=disp, y=mpg)) +
   geom_point() +
   ggtitle("disp vs. mpg")

#display both plots on same page using 2 columns
multiplot(p1, p2, cols=2)

This produces the following result:

multiplot() function in R with two columns

Since we specified col=2, the plots are shown side-by-side in 2 columns.

If we simply leave out the col argument, then the plots will be shown in a vertical format in one column:

#load necessary packages
library(ggplot2)
library(scater)

#create first plot
p1 <- ggplot(mtcars, aes(x=disp, y=qsec)) +
   geom_point() +
   ggtitle("disp vs. qsec")

#create second plot
p2 <- ggplot(mtcars, aes(x=disp, y=mpg)) +
   geom_point() +
   ggtitle("disp vs. mpg")

#display both plots on same page using 1 column
multiplot(p1, p)

This produces the following result:

multiplot function in R with one column

It’s worth noting that if the multiplot() function is not working or if you’re unable to load the scater package, then you can use the following syntax to manually define the multiplot() function:

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  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 {
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    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))
    }
  }
}

You can then skip loading the scater package and directly use the multiplot() function to plot multiple ggplot2 plots on the same page.

Note: In each of these examples we used the multiplot() function to produce two ggplot2 plots but you can specify as many ggplot2 plots as you’d like within the function to display even more plots on the same page.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Change the Legend Title in ggplot2
How to Rotate Axis Labels in ggplot2
How to Adjust Space Between Bars in ggplot2

Leave a Reply

Your email address will not be published. Required fields are marked *