Created
September 14, 2012 21:30
-
-
Save stephenturner/3724991 to your computer and use it in GitHub Desktop.
Arrange 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
## Function for arranging ggplots. use png(); arrange(p1, p2, ncol=1); dev.off() to save. | |
require(grid) | |
vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y) | |
arrange_ggplot2 <- function(..., nrow=NULL, ncol=NULL, as.table=FALSE) { | |
dots <- list(...) | |
n <- length(dots) | |
if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol = ceiling(n/nrow)} | |
if(is.null(nrow)) { nrow = ceiling(n/ncol)} | |
if(is.null(ncol)) { ncol = ceiling(n/nrow)} | |
## NOTE see n2mfrow in grDevices for possible alternative | |
grid.newpage() | |
pushViewport(viewport(layout=grid.layout(nrow,ncol) ) ) | |
ii.p <- 1 | |
for(ii.row in seq(1, nrow)){ | |
ii.table.row <- ii.row | |
if(as.table) {ii.table.row <- nrow - ii.table.row + 1} | |
for(ii.col in seq(1, ncol)){ | |
ii.table <- ii.p | |
if(ii.p > n) break | |
print(dots[[ii.table]], vp=vp.layout(ii.table.row, ii.col)) | |
ii.p <- ii.p + 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment