简体   繁体   English

绘制“布尔值”栅格并将 NA 值添加到图例

[英]Plot "boolean" raster and add NA-Values to legend

I have a raster with values that are either 1 or NA .我有一个值为1NA的栅格。 By default the plot(raster_1) -call gives me a continuous legend.默认情况下, plot(raster_1)给了我一个连续的图例。 What I actually want is a simple categorical legend with two entries and colors.我真正想要的是一个带有两个条目和颜色的简单分类图例。 One being 1 the other one NA .一个是1另一个是NA I converted my raster to a categorical object with as.factor(raster_1) .我使用as.factor(raster_1)将我的栅格转换为分类对象。 But now I don't know how I can generate this simple legend... My raster-values look like this:但现在我不知道如何生成这个简单的图例......我的栅格值如下所示:

source     : memory
names      : raster_1 
values     : 1, 1  (min, max)
attributes :
 ID
  1

Set legend=FALSE and colNA other than the default 'transparent' in the call to plot and then add a custom legend where you can set xpd=TRUE , so the legend can be placed outside the plot margins.在调用plot设置legend=FALSEcolNA而不是默认的“透明”,然后添加一个自定义legend ,您可以在其中设置xpd=TRUE ,这样图例就可以放置在图边距之外。 inset = c(-0.3,0) can be adjusted to your preference in the call to legend inset = c(-0.3,0)可以在调用legend根据您的喜好进行调整

r <- raster(ncol=10, nrow=10, xmx=-80, xmn=-150, ymn=20, ymx=60) #toy raster
values(r) <- c(rep(1, 30), rep(NA, 30), rep(1, 40)) #add 1 and NA values
r
#class      : RasterLayer 
#dimensions : 10, 10, 100  (nrow, ncol, ncell)
#resolution : 7, 4  (x, y)
#extent     : -150, -80, 20, 60  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#source     : memory
#names      : layer 
#values     : 1, 1  (min, max)
plot(r, col='red', colNA='black', legend=FALSE)
legend('topright', legend = c('1', 'NA'), pch=15, col=c('red', 'black'), inset = c(-0.3,0), xpd = TRUE)

在此处输入图片说明

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

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