Created
July 25, 2012 21:15
-
-
Save truncs/3178708 to your computer and use it in GitHub Desktop.
R multiplot
This file contains 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
multiplot <- function(..., plotlist=NULL, cols) { | |
require(grid) | |
# Make a list from the ... arguments and plotlist | |
plots <- c(list(...), plotlist) | |
numPlots = length(plots) | |
# Make the panel | |
plotCols = cols # 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 )) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment