首页 > 解决方案 > 在 R 语言中结合栅格图例

问题描述

很抱歉,我无法解决这个明显愚蠢的问题,即使在谷歌搜索了一段时间后也是如此。让我们假设以下情况,其中我有两个重叠但值等级不同的栅格。

library(raster)
# A couple of rasters from scratch
r2 <- r1 <- raster(nrows=10, ncols=10)
r1[] <- sample(c(0:99), 100, replace = F)
r2[] <- sample(c(50:149), 100, replace = F)
par(mfrow = c(1,2))
plot(r1)
plot(r2)

在此处输入图像描述

如何创建相同的图,但只有一个从 0 到 149 的图例,即两个栅格的组合排名?

标签: rlegendrasterlegend-properties

解决方案


根据我在上一条评论中分享的答案,您可以手动设置图例的中断和颜色。然后,您只需将该色带应用于两个绘图。这是代码和情节。

# Adjust plot margins
par(mar=c(2,2,2,5))

# Set manually the breaks
breaks = seq(0,150,25)
pal <- colorRampPalette(c("white","orange","yellow","green","forestgreen"))

plot(r1, breaks=breaks, col = pal(length(breaks)-1)) 
plot(r2, breaks=breaks, col = pal(length(breaks)-1)) 

地块


推荐阅读