简体   繁体   English

带有类别图例的多图窗口中的栅格水平图

[英]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. 我正在尝试在R中制作一个包含多个不同栅格的多面板图。 I am working on a Windows machine. 我正在Windows机器上工作。

The rasters are to displayed in the following format: 栅格将以以下格式显示:

First row should contain a single raster (centered), with a visible "main" title and ylabel. 第一行应包含一个栅格(居中),并带有可见的“主”标题和ylabel。

Rows 2 through 6 contain two adjacent rasters (ie two columns). 第2至6行包含两个相邻的栅格(即,两列)。 The "main" titles are only needed on row 2, as the headings hold for the remaining rows. 仅在第2行上需要“主要”标题,因为标题适用于其余行。

Row 7 should contain a categorical legend. 第7行应包含分类图例。 Similar to that described in the answer provided here: 与此处提供的答案中所述的内容类似:

https://gis.stackexchange.com/questions/73143/legend-of-a-raster-map-with-categorical-data 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. 我想最小化第2行和第8行之间的距离,同时保留第1行和第2行之间的一些额外空间以保留标题。 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. 我不确定为什么会产生“主要”标题的彩色背景,并且是在尝试使用“ top.padding”和“ bottom.padding”命令减少空白之后发生的。


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: 然后,我想在第7行中添加与以下内容类似的图例:

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? 如何在增加第1行和第2行之间的间距的同时减小第2行和第6行之间的间距? How to now add in the categorical legend in row 7? 现在如何在第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 . 条形区域的背景色是lattice的默认值。 The rasterTheme sets it to white but you have to use par.settings instead of col.regions as described here . rasterTheme其设置为白色,但你必须使用par.settings代替col.regions描述这里
  2. You should try the panel.width and panel.height arguments of print.trellis . 你应该尝试的panel.widthpanel.height的参数print.trellis
  3. levelplot is able to plot categorical rasters with an appropriate legend, but you have to define the raster accordingly. levelplot能够绘制具有适当图例的分类栅格,但是您必须相应地定义栅格。 Here you will find an example. 在这里,您将找到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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