Skip to content

Instantly share code, notes, and snippets.

@idot
Last active December 16, 2015 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idot/5346145 to your computer and use it in GitHub Desktop.
Save idot/5346145 to your computer and use it in GitHub Desktop.
combine ggplot2 plots
library(gridExtra)
getLegend <- function(tp.gplot, position="bottom", box="horizontal"){
tmp <- ggplot_gtable(ggplot_build(tp.gplot + theme(legend.position = position, legend.box = box)))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
#then extract height or width of legend
legend <- getLegend(p1)
lheight <- sum(legend$height)
#arrange with grid
p <- grid.arrange(arrangeGrob(p1+theme(legend.position="none"),
p2+theme(legend.position="none"),
p3+theme(legend.position="none"),
p4+theme(legend.position="none"),
main=bpl$primer),
legend,
heights=unit.c(unit(1, "npc") - lheight, lheight),
nrow=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment