R/grid_arrange_shared_legend.R

Defines functions grid_arrange_shared_legend

Documented in grid_arrange_shared_legend

#' grid arrange (from gridExtra) with common legend
#' @param ... list of ggplot objects
#' @param nrow number of plot rows
#' @param ncol number of plot columns (defaults to nr of plots)
#' @param position position of legend.
#' @export
#' @examples
#' grid_arrange_shared_legend(p1,p2,p3,p4, nrow=2,ncol=2)

grid_arrange_shared_legend <- function(..., nrow = 1, ncol = length(list(...)), position = c("bottom", "right")) {
  require(ggplot2)
  require(gridExtra)
  require(grid)
  plots <- list(...)
  position <- match.arg(position)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position = "none"))
  gl <- c(gl, nrow = nrow, ncol = ncol)

  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
  grid.newpage()
  grid.draw(combined)

}
josiahpjking/jkr documentation built on Sept. 7, 2020, 3:54 a.m.