简体   繁体   English

R中光栅图中黑色的基图NA值

[英]base plot NA values in black color in raster map in R

I have a rasterized S4 object, created by using rasterize function onto a dataset.我有一个光栅化的 S4 对象,它是通过在数据集上使用 rasterize 函数创建的。 The dataset that had been put into it has some NA values.放入其中的数据集有一些 NA 值。 I would like to depict these NA cells in black color when using the base plot function in R.在 R 中使用基本绘图函数时,我想用黑色描绘这些 NA 单元格。

Tried the following code:尝试了以下代码:

library(RColorBrewer)

my.colors = colorRampPalette(rev(brewer.pal(9, "RdBu")))

plot(rasterized object, col= ifelse(is.na(rasterized object@data@values),"black", my.colors(255)), box=F, add=F)

# col(ifelse) was inspired from Plot conditional color with NA data # col(ifelse) 的灵感来自使用 NA 数据绘制条件颜色

That doesn't work and many grid cells with real values get depicted in black instead.这不起作用,许多具有真实值的网格单元被用黑色描绘。

There are no error messages.没有错误消息。

What could be changed, given I want to use basic plot syntax of R and not ggplot ?鉴于我想使用R 的基本绘图语法而不是 ggplot ,可以改变什么?

I guess that you are using the raster package and that the rasterized S4 object is a RasterLayer我猜你正在使用raster包并且光栅化的 S4 对象是一个RasterLayer

If so, the plot function of the raster package (version 3.4.5) has a colNA argument.如果是这样,光栅包(版本 3.4.5)的plot函数有一个colNA参数。

plot(r, col = my.colors(255), colNA = "black")

Here is a reproducible example:这是一个可重现的示例:

library(raster)
library(RColorBrewer)
r <- raster(nrows=10, ncols=10)
r <- setValues(r, c(rep(NA, 10), 1:90)) 

my.colors = colorRampPalette(rev(brewer.pal(9, "RdBu")))

plot(r, col = my.colors(255), colNA = "black")

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

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