简体   繁体   English

如何为rasterStack中的所有栅格图层显示相同的图例

[英]How to display the same legend for all raster layers in a rasterStack

I've looked through all SO but haven't been able to find an answer to this specific question: 我已经浏览了所有SO,但无法找到这个特定问题的答案:

I have a rasterStack with several layers whose values span over a quite large range. 我有一个带有几个图层的rasterStack,其值跨很大的范围。 Knowing the values of each layer and the chosen colour scale I believe I have managed to plot all the rasters with the same colour scale, but I face three problems now: 知道了每个图层的值和所选的色阶之后,我相信我已经设法绘制了具有相同色阶的所有栅格,但是现在我面临三个问题:

  1. I can't be sure that the values are being plotted with the same colour scale, although it seems like it 我不确定这些值是用相同的色标绘制的,尽管看起来像这样
  2. I can't manage to plot the same scale and scale labels for all layers. 我无法为所有图层绘制相同的比例尺和比例尺标签。
  3. One of my cells in ras3, ras3[3,9] , doesn't get coloured, but it is not NA! 我在ras3中的一个细胞 ras3[3,9]不会着色,但不是NA!

Example script follows: 示例脚本如下:

library(raster)
# generate example rasters
set.seed(123)
ras1 <- raster(ncol = 10, nrow= 10)
values(ras1) <- runif(100, 1, 10)

ras2 <- raster(ncol = 10, nrow = 10)
values(ras2) <- runif(100, 5, 50)

ras3 <- raster(ncol = 10, nrow = 10)
values(ras3) <- runif(100, 10, 100)

# stack them 
rasStack <- stack(ras1, ras2, ras3)
# plot normally to check the values
plot(rasStack)

# obtain max and min values 
maxv <- max(maxValue(rasStack))+1
minv <- min(minValue(rasStack))

# set the breaks between min and max values
brks <- seq(minv,maxv,by=0.1)
nbrks <- length(brks)-1
r.range <- c(minv, maxv)

# generate palette
colfunc<-colorRampPalette(c("springgreen", "royalblue", "yellow", "red"))

# plot in a loop with a common legend, using legend.only = T    
for(i in seq_len(nlayers(rasStack))){
  tmp <- rasStack[[i]]
  plot(tmp, breaks=brks,col=colfunc(nbrks), legend = F, zlim=c(minv,maxv), 
  main = names(tmp))
  plot(tmp, legend.only=TRUE, col=colfunc(nbrks),
       legend.width=1, legend.shrink=0.75,
       axis.args=list(at=seq(minv, maxv, 5),
                  labels=round(seq(r.range[1], r.range[2], 5), 2), 
                  cex.axis=0.6),
   legend.args=list(text='value', side=4, font=2, line=2.5, cex=0.8))}

# check that the blank cell has a valid value
ras3[3, 9]
> 99.01704 

Any help will be greatly appreciated! 任何帮助将不胜感激!


EDIT: as per ycw's answer I've edited the code and now problem no. 编辑:根据ycw的答案,我已经编辑了代码,现在没有问题。 3 has disappeared! 3已经消失了!

I just fixed this problem so I'll post the solution in case someone else stumbles with this. 我只是解决了这个问题,所以我将发布解决方案,以防其他人迷路了。

I might be a bit of a workaround, and it certainly is not very elegant, but it works: 我可能有一个解决方法,它当然不是很优雅,但是可以工作:

First of all we add up all three raster layers in a new one 首先,我们将所有三个栅格图层加在一起

rasTot <- ras1 + ras2 + ras3

Now we run the loop from the previous code but in the plot with the legend.only call we use this total raster. 现在我们从之前的代码开始运行循环,但是在带有legend.only的绘图中。 legend.only调用时使用此总栅格。

for(i in seq_len(nlayers(rasStack))){
  tmp <- rasStack[[i]]
  plot(tmp, breaks=brks,col=colfunc(nbrks), legend = F, zlim=c(minv,maxv), 
  main = names(tmp))
  plot(rasTot, legend.only=TRUE, col=colfunc(nbrks),
       legend.width=1, legend.shrink=0.75,
       legend.args=list(text='value', side=4, font=2, line=2.5, cex=0.8))
}

I also edited out some of the legend label specifications, as the defaults are OK. 我还编辑了一些图例标签规范,因为默认设置还可以。

The last break number should be larger than the maximum value of your data ( maxv ) so that the cell with the maximum can be colored based on the last color category. 最后一个中断编号应大于数据的最大值( maxv ),以便可以根据最后一个颜色类别对具有最大值的单元进行着色。 Otherwise, the cell will be blank. 否则,该单元格将为空白。

I modified your code by changing maxv <- max(maxValue(rasStack)) + 1 but did not change other parts. 我通过更改maxv <- max(maxValue(rasStack)) + 1修改了您的代码,但没有更改其他部分。 The result looks good. 结果看起来不错。

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

相关问题 从栅格堆栈中删除具有许多NA的栅格图层 - Remove raster layers with many NAs from rasterstack 基于栅格层网格单元数差异的子集 R 栅格堆栈 - Subset R rasterstack based on difference in raster layers grid cell numbers 如何处理大型“ RasterStack”对象并将所有栅格网格写入R中的纯文本数据中? - How to manipulate large `RasterStack` object and write all raster grid in plain-text data in R? 使用RasterStack进行焦点{raster}回归 - focal {raster} regression with RasterStack 为什么从 SpatRaster 转换为 rasterStack 时栅格图层的数据会发生变化? - Why do my raster layers' data change when converting from SpatRaster to rasterStack? 如何从 RasterStack 中提取像元值并将其应用于新栅格 - How to extract cell values from RasterStack and apply them to a new raster 如何自动从R中的栅格堆栈中删除空栅格? - How to automatically remove an empty raster from a rasterstack in r? 光栅包如何识别图像的所有波段(层)? - How to do the raster package identify all bands (layers) of a image? 如何为 R 中的每一列创建一个单独的数据框,然后将所有栅格层栅格化并将其绑定到多层栅格中? - How to create an individual dataframe for each column in R to then rasterize and bind all raster layers into multi-layer raster? 使用ggplot在图例中包含所有图层 - Having all layers in the legend with ggplot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM