简体   繁体   English

如何在R中将某些值设置为特定颜色?

[英]How to set certain values to specific colour in R?

I have one matrix r (raster). 我有一个矩阵r(栅格)。 I would like to plot r , this simply can be done using 我想绘制r,这可以简单地使用

plot (r)

But I would like to mark all values of 20 (so this value will not be taken into account in the scale of legend) to red colour and plot r normally as below. 但是我想将所有值20标记为红色(因此在图例比例中将不考虑该值),将其标记为红色,并正常绘制r如下所示。 a reproducible example: 一个可重现的例子:

library(raster)
r <- raster(nrows=10, ncols=10)
r <- setValues(r, 1:ncell(r))
plot(r)

This will produce this: 这将产生以下结果:

在此处输入图片说明

elegant is different but is this what you intend to do? 优雅与众不同,但这是您打算做什么?

par(mfrow=c(2,2))
plot(r)
r.20 <- calc(r, fun=function(x){ x[x == 20] <- NA; return(x)} )
as.matrix(r.20)
plot(r.20)
r.not20 <- calc(r, fun=function(x){ x[x != 20] <- NA; return(x)} )
plot(r.not20, col="red")
plot(r.20);
par(new=TRUE)
plot(r.not20, col="red", legend=FALSE)

在此处输入图片说明

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

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