rplotrastergridextrarastervis

Organizing raster plot in R using rasterVis and gridExtra


I am having a tough time plotting maps together in R using rasterVis and gridExtra. This is how my code looks like (sorry, it is certainly not elegant):

require(rasterVis)
require(gridExtra)

p1 <- levelplot(avit_bac12, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p2 <- levelplot(avit_bac17, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p3 <- levelplot(avit_eng, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p4 <- levelplot(avit_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p5 <- levelplot(bac12_bac17, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p6 <- levelplot(bac12_eng, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p7 <- levelplot(bac12_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p8 <- levelplot(bac17_eng, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p9 <- levelplot(bac17_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p10 <- levelplot(eng_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))

p.avit <- levelplot(avit, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Avitabile et al. (2016)", scales=list(draw=FALSE))
p.bac12 <- levelplot(bac12, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Baccini et al. (2012)", scales=list(draw=FALSE))
p.bac17 <- levelplot(bac17, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Baccini et al. (2017)", scales=list(draw=FALSE))
p.eng <- levelplot(eng, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Englund et al. (2017)", scales=list(draw=FALSE))
p.sf <- levelplot(sf, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Soares-Filho et al. (2017)", scales=list(draw=FALSE))

p.list <- list(p.avit, p.bac12, p.bac17, p.eng, p.sf,
               p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)

lay <- rbind(c(NA,  2,  3,  4,  5),
             c(1,   6,  7,  8,  9),
             c(2,   NA, 10, 11, 12),
             c(3,   NA, NA, 13, 14),
             c(4,   NA, NA, NA, 15))

grid.arrange(grobs=p.list, layout_matrix=lay,
         vp = grid::viewport(width=0.4,height=0.3))

I was hoping it would return something like this:enter image description here

... but instead, the size of the maps get messed up and I get this: enter image description here

Any ideas on how to solve this problem?


Solution

  • The problem is that some numbers in layout_matrix are repeated. Repeated numbers means that a plot should cover the cells in the grid with the same number. Since each of your plots should take up a single cell in the grid, no numbers in layout matrix should be repeated. If a plot needs to be placed in two locations, pass it to grid.arrange twice, but make sure layout_matrix contains unique numbers (and NAs if needed). Easier with examples:

    library(ggplot2)
    library(gridExtra)
    p <- ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length, color=Species)) + geom_point(show.legend = FALSE)
    
    lay1 <- rbind(c(NA, 1,2), 
                  c(2,3, NA))
    grid.arrange(p,p,p, layout_matrix=lay1) 
    # adding a 4th plot in the above grid.arrange call results with error
    
    lay2 <- rbind(c(NA, 1,2), 
                  c(3,4, NA))
    grid.arrange(p,p,p,p, layout_matrix=lay2)
    
    lay3 <- rbind(c(1,1,NA,2),
                  c(1,1,3, NA))
    grid.arrange(p,p,p, layout_matrix=lay3)
    

    The three examples should give the following plots:

    lay1:

    enter image description here

    lay2:

    enter image description here

    lay3:

    enter image description here