0

I want to plot 10 rasters using levelplot but I want to arrange the plots in such a way that there's 1 plot in first row (in the center) and then 3 plots in next three rows.

#Desired Output looks something like this
     1
2    3    4
5    6    7
8    9    10

I am using the below code but it plots them sequentially, one after the other (obviously).

library(raster)
library(rasterVis)
library(lattice)
library(grid)
library(RColorBrewer)

#sample data
s <- stack(replicate(10, raster(matrix(runif(100), 10))))
col1 <- colorRampPalette(c("#00008B", "#1874CD", "#00BFFF", "#76EE00", "#006400", "#FFB90F", "#EE7600", "#8B2323"))
mk.attr = c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October')

levelplot(s, xlab = NULL, ylab = NULL, col.regions = col1, names.attr = mk.attr,
          colorkey = list(space = "right", labels = list(cex=1, font=2, col="brown"), 
                          height=1, width=1.4, 
                          row=4, column=3, hjust = 2), 
          main = list(label = "Main Title", cex = 1.2), margin = FALSE)

#add axis title
grid::grid.text(label = "Trends (in mm/year)", x = unit(0.90, "npc"), y = unit(0.50, "npc"), rot = 90,
                gp = gpar(fontface = "bold", col = "gray33", fontsize = 11))

Edit:

Taking the cue from @Oscar's comment, I adapted a grid.arrange solution for this. But now I don't know how to do two things.

  1. How to put a common legend instead of one for every plot?
  2. How to define a rasterTheme using custom colors (and not just some color combo from brewer.pal?

Updated code:

p = list()
   
for (i in 1:nlayers(s)) {
  p[[i]] = levelplot(s[[i]],
                     main = hr.attr[i], 
                     colorkey = T, 
                     margin = F)
}

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

gridExtra::grid.arrange(grobs = p, layout_matrix = lay, top = 'TRY')
Ray
  • 56
  • 1
  • 7
  • Take a look to the two answers to [this question](https://stackoverflow.com/questions/62730787/rastervis-setting-the-bottom-plots-in-the-middle-with-levelplot) and to [this solution](https://stackoverflow.com/questions/52953569/ggplot2-correctly-arrange-odd-number-of-plots-into-one-figure/52954119#52954119). – Oscar Perpiñán Jul 27 '20 at 17:31
  • Thanks for the link. I adapted a solution using ```grid.arrange```. I was wondering is there a way to have a common legend instead of each plot having one for its own? – Ray Jul 27 '20 at 19:43

0 Answers0