简体   繁体   中英

R raster levelplot in multiplot window w/ categorical legend

I am attempting to make a multipanel plot in R that contains a number of different rasters. I am working on a Windows machine.

The rasters are to displayed in the following format:

First row should contain a single raster (centered), with a visible "main" title and ylabel.

Rows 2 through 6 contain two adjacent rasters (ie two columns). The "main" titles are only needed on row 2, as the headings hold for the remaining rows.

Row 7 should contain a categorical legend. Similar to that described in the answer provided here:

https://gis.stackexchange.com/questions/73143/legend-of-a-raster-map-with-categorical-data

The spacing is important. I would like to minimize the distance between rows 2 and 8, while preserving some extra space between row 1 and 2 to allow for the titles. Each raster should be the same size.

Please find a reproducible example of what I'm working on below. I'm unsure why the colored background to the "main" titles are produced, and occurred after I attempted to reduce the white space with the "top.padding" and "bottom.padding" commands.


library(raster)

library(rasterVis)

f <- system.file("external/test.grd", package="raster")

r <- raster(f)

rmat = as.matrix(r) 

pc = c(0,0.6,0.8,0.9,0.92,0.95,0.97,0.98,0.99,1) 

digits_n = c(0,1,c(rep(0,length(pc)-2)))

color_breaks = NULL

for (i in 1:length(pc)){color_breaks = c(color_breaks, signif(quantile(c(rmat[which(!is.na(rmat))]),pc[i]), digits = digits_n[i]))}

color_breaks = unique(color_breaks)

color_type = c(colorRampPalette(c("snow1","snow3","seagreen","orange","firebrick"))(length(color_breaks))) 

ss = stack(r)

p0 = levelplot(ss, col.regions=color_type, colorkey=FALSE,margin=FALSE, at=color_breaks,names.attr=c(2000), xlab=NULL, ylab=c("A0"), scales=list(draw=FALSE), par.settings=list(layout.heights=list(bottom.padding=-2)))

ss = stack(r,r)

p1 = levelplot(ss,col.regions=color_type, colorkey=FALSE, margin=FALSE, at=color_breaks, names.attr=c(2050,2100), xlab=NULL, ylab=c("A1"), scales=list(draw=FALSE), layout=c(2,1), par.settings=list(layout.heights=list(top.padding=-2,bottom.padding=-2)))

ss = stack(r,r)

p2 = levelplot(ss,col.regions=color_type, colorkey=FALSE, margin=FALSE, at=color_breaks, names.attr=c("",""), xlab=NULL, ylab=c("A2"), scales=list(draw=FALSE), layout=c(2,1), par.settings=list(layout.heights=list(top.padding=-2,bottom.padding=-2))) 

ss = stack(r,r)

p3 = levelplot(ss,col.regions=color_type, colorkey=FALSE, margin=FALSE, at=color_breaks, names.attr=c("",""), xlab=NULL, ylab=c("A3"), scales=list(draw=FALSE), layout=c(2,1), par.settings=list(layout.heights=list(top.padding=-2,bottom.padding=-2))) 

ss = stack(r,r)

p4 = levelplot(ss,col.regions=color_type, colorkey=FALSE, margin=FALSE, at=color_breaks, names.attr=c("",""), xlab=NULL, ylab=c("A4"), scales=list(draw=FALSE), layout=c(2,1), par.settings=list(layout.heights=list(top.padding=-2,bottom.padding=-2)))

ss = stack(r,r)

p5 = levelplot(ss,col.regions=color_type, colorkey=FALSE, margin=FALSE, at=color_breaks, names.attr=c("",""), xlab=NULL, ylab=c("A5"), scales=list(draw=FALSE), layout=c(2,1), par.settings=list(layout.heights=list(top.padding=-2,bottom.padding=-2))) 

windows()

print(p0, split= c(1,1,1,7), between = c(3,3))

print(p1, split= c(1,2,1,7), newpage=FALSE)

print(p2, split= c(1,3,1,7), newpage=FALSE)

print(p3, split= c(1,4,1,7), newpage=FALSE)

print(p4, split= c(1,5,1,7), newpage=FALSE)

print(p5, split= c(1,6,1,7), newpage=FALSE)

I would then like to add in the legend in row 7 with something similar to the following:

legend("center", 
    ncol = length(color_breaks)/2,
    legend=c(color_breaks),
    fill=color_type,cex=0.6, 
    title=expression(bold("Magnitude [ UNITS ]")), box.col = FALSE, xjust = 0, title.adj = 0
    )

I would post the image I obtain but I guess I need reputation points.

Why has the background color in the titles appeared? How can I reduce the spacing between rows 2 and 6 while increasing the space between row 1 and 2? How to now add in the categorical legend in row 7?

Thank you.

I will try to answer briefly your questions:

  1. The background color of the strip regions is the default value in lattice . The rasterTheme sets it to white but you have to use par.settings instead of col.regions as described here .
  2. You should try the panel.width and panel.height arguments of print.trellis .
  3. levelplot is able to plot categorical rasters with an appropriate legend, but you have to define the raster accordingly. Here you will find an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM