rrasterlevelplot

levelplot multiplot with margins, without blank spaces


I'm having difficulties creating a multiplot using the levelplot function with margins.

I face at least one of these issues with every attempt: too much white space, error, or too large a margin.

library(raster)
library(rasterVis)
library(gridExtra)

f <- system.file("external/test.grd", package="raster")
r <- raster(f)
x <- levelplot(r, margin=T)


# ATTEMPT 1
print(x, split = c(1, 1, 2, 2), more = TRUE)
print(x, split = c(2, 1, 2, 2), more = TRUE)
print(x, split = c(1, 1, 1, 2), more = FALSE)

# ATTEMPT 2 - error

l <- rasterVis::levelplot(raster::stack(list(x, x, x)), layout=c(3,1), margin = T);  l

# ATTEMPT 3 = best, but a lot of white space and bigger margin

grobs <- arrangeGrob(grobs=list(x, x, x), ncol=3)
grid.arrange(grobs)

i'm wanting something like this: (bonus if with commom legend)

enter image description here

ty !


Solution

  • After a bit of digging, you can modify parts of the lattice plot e.g. R remove whitespace between levelplot grobs

    I've tried it on your x object and it looks better. I added a bit more margin back in as their default removed the legend text e.g. try

    x <- levelplot(r, margin=TRUE)
    
    x$par.settings$layout.heights[
      c( 'bottom.padding',
         'top.padding',
         'key.sub.padding',
         'axis.xlab.padding',
         'key.axis.padding',
         'main.key.padding') ] <- 1
    x$aspect.fill <- TRUE
    
    grobs <- arrangeGrob(grobs=list(x, x, x), ncol=3)
    grid.arrange(grobs)