1

I'm using the rasterVis package and the levelplot function to plot seven rasters on a single plot.

Here is the layout I'm going for: correct layout

I've achieved this using the following code:

png("E:/all_files/production/plots/for_final/hist_vis_split2.png", 
width=6, height=9, units="in", res=72)
layout(matrix(c(1,1,2,3,4,5,6,7), 4, 2, byrow = T))
print(maurer+layer(sp.lines(sr)), split=c(2,1,3,4), more=TRUE)
print(nar.ccsm+layer(sp.lines(sr)), split=c(1,2,3,4), more=TRUE)
print(nar.gfdl+layer(sp.lines(sr)), split=c(3,2,3,4), more=TRUE)
print(bcsd.ccsm+layer(sp.lines(sr)), split=c(1,3,3,4), more=TRUE)
print(bcsd.gfdl+layer(sp.lines(sr)), split=c(3,3,3,4), more=TRUE)
print(bcca.ccsm+layer(sp.lines(sr)), split=c(1,4,3,4), more=TRUE)
print(bcca.gfdl+layer(sp.lines(sr)), split=c(3,4,3,4))
dev.off()

Besides being clunky and lacking control, this is still missing a main title and a single common colorkey for the entire image.

I would prefer to use the levelplot command. Below is the code I've used to plot all seven rasters in a single levelplot with a single colorkey. Unfortunately this does not have the correct layout:

incorrect layout

crop.stack <- stack(maurer, bcsd.ccsm.crop, bcsd.gfdl.crop, bcca.ccsm.crop, 
                bcca.gfdl.crop, nar.ccsm.crop, nar.gfdl.crop)
plot.names <- c("Maurer", "BCSD CCSM", "BCSD GFDL", "BCCA CCSM", 
            "BCCA GFDL", "NARCCAP CCSM", "NARCCAP GFDL")

png("E:/all_files/production/plots/for_final/hist_vis.png", 
    width=6, height=9, units="in", res=72)
hist <- levelplot(crop.stack, main="Historical Average Production Days", 
          col.regions=cols, 
          names.attr=plot.names, 
          scales=list(draw=F), 
          layout=c(2,4))
hist + layer(sp.lines(sr))
dev.off()

The solution here is close, but does not deal with and odd number of plots. How can I achieve the desired layout within the levelplot command?

www
  • 38,575
  • 12
  • 48
  • 84
em.d.kear
  • 33
  • 8
  • Might be worth checking out `gridExtra::grid.arrange`: https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html – jbaums Sep 03 '16 at 04:06

0 Answers0