[英]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: 知道了每个图层的值和所选的色阶之后,我相信我已经设法绘制了具有相同色阶的所有栅格,但是现在我面临三个问题:
ras3[3,9]
, doesn't get coloured, but it is not NA!
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.