Last active
December 16, 2015 00:18
-
-
Save idot/5346145 to your computer and use it in GitHub Desktop.
combine ggplot2 plots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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