多色窗口中的R栅格级别图,带有分类图例

时间:2015-01-22 15:35:21

标签: r layout legend raster levelplot

我试图在R中创建一个包含许多不同栅格的多面板图。我正在使用Windows机器。

栅格将以以下格式显示:

第一行应包含单个栅格(居中),带有可见的“主”标题和ylabel。

第2行到第6行包含两个相邻的栅格(即两列)。 “主要”标题仅在第2行需要,因为标题适用于剩余的行。

第7行应包含分类图例。与此处提供的答案中描述的类似:

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

间距很重要。我想最小化第2行和第8行之间的距离,同时在第1行和第2行之间保留一些额外的空格以允许标题。每个栅格的大小应相同。

请在下面找到我正在处理的可重现的示例。我不确定为什么会出现“主要”标题的彩色背景,并且在我尝试使用“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)

然后我想在第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
    )

我会发布我获得的图像,但我想我需要声望点。

为什么标题中的背景颜色出现了?如何在增加第1行和第2行之间的空间的同时减少第2行和第6行之间的间距?现在如何添加第7行中的分类图例?

谢谢。

1 个答案:

答案 0 :(得分:1)

我将尝试简要回答您的问题:

  1. 条带区域的背景颜色是lattice中的默认值。 rasterTheme将其设置为白色,但您必须使用par.settings代替col.regions,如here所述。
  2. 您应该尝试panel.width的{​​{1}}和panel.height个参数。
  3. print.trellis能够使用适当的图例绘制分类栅格,但您必须相应地定义栅格。 Here你会找到一个例子。