R/multiplot.R

Defines functions multiplot

Documented in multiplot

#' draw multile ggplot graphs
#'
#' @param ... ggplot objects
#' @param plotlist of a list of ggplot
#' @param ncol     numbre of column
#'
#' @export
#'
multiplot <- function(..., plotlist=NULL, ncol) {
   require(grid)
   
   # Make a list from the ... arguments and plotlist
   plots <- c(list(...), plotlist)
   
   numPlots = length(plots)
   
   # Make the panel
   plotCols = ncol                         # Number of columns of plots
   plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
   
   # Set up the page
   grid.newpage()
   pushViewport(viewport(layout = grid.layout(plotRows,
                                              plotCols)))
   vplayout <- function(x, y)
      viewport(layout.pos.row = x, layout.pos.col = y)
   
   # Make each plot, in the correct location
   for (i in 1:numPlots) {
      curRow = ceiling(i/plotCols)
      curCol = (i-1) %% plotCols + 1
      print(plots[[i]], vp = vplayout(curRow, curCol ))
   }
}
GabrielPigeon/gabtool documentation built on May 6, 2019, 5:32 p.m.