latticelayout <- function(plotlist,R,C) { #Sets up an R x C grid and plots the elements from #plotlist in order, going across rows first. ##USAGE EXAMPLE #source("http://jinome.stanford.edu/stat366/rcode/latticelayout.R") #pl1 <- densityplot(rnorm(1000)) #pl2 <- densityplot(rexp(1000)) #latticelayout(list(pl1,pl2),2,1) if(length(plotlist) != (R*C)) { print("Number of plots is not equal to row * column dimensions of layout"); return; } library(lattice) library(grid) grid.newpage() pushViewport(viewport(layout=grid.layout(R,C))) n <- 1; for(r in 1:R) { for(c in 1:C) { pushViewport(viewport(layout.pos.col=c, layout.pos.row=r)) print(plotlist[[n]], newpage = FALSE) popViewport(1) n <- n+1; } } }