简体   繁体   English

栅格颜色-正红色和负蓝色

[英]Raster color - positive red & negative blue

I have a spatial raster create in R with positive and negative values. 我在R中创建了具有正值和负值的空间栅格。 I want for all positive values a red color ramp and for all negative values a blue color ramp. 我希望所有正值都为红色,而所有负值都为蓝色。 Basically I want to define my own color ramp with zero as blank, positive values in red, and negative values in blue (these can be any colors). 基本上,我想定义自己的颜色渐变,其中零为空白,红色为正值,蓝色为负值(这些可以是任何颜色)。 Most color ramps in R spread all available colors between the min. R中的大多数色带在最小值之间散布所有可用的颜色。 and max. 和最大 value. 值。

I do not seem to be able to find the solution, so hopefully someone here has suggestions. 我似乎无法找到解决方案,因此希望这里有人提出建议。

Here's an example for a raster with values between -5 and 5 for which I need a 'new' col definition: 这是一个值在-5到5之间的栅格的示例,为此我需要一个“新” col定义:

library(raster)
set.seed(9)
r <- raster(ncol=10, nrow=10)
r1 <- setValues(r, (1:ncell(r))/10 + rnorm(ncell(r)) - 5)
plot(r1, col= topo.colors(12))

Best -- Niels 最好-尼尔斯

I could not find a more elegant way than adding a tolerance, because colours should be given to a segment not a single point (eg 0) if you get a more elegant way, please go ahead 除了增加公差外,我找不到其他更优雅的方法,因为如果要获得更优雅的方法,则应该为段指定颜色而不是单个点(例如0),请继续

library(raster)
set.seed(9)
r <- raster(ncol=10, nrow=10)
r1 <- setValues(r, (1:ncell(r))/10 + rnorm(ncell(r)) - 5)
tol=10^-12
breakpoints <- c(min(as.data.frame(r1)),0-tol,0+tol,max(as.data.frame(r1)))
colors <- c("blue","white","red")
r1.range <- c(minValue(r1), maxValue(r1))
plot(r1,breaks=breakpoints,col=colors,     axis.args=list(at=c(r1.range[1], 0,r1.range[2])))

The axis.args argument is only needed to make the legend look nice, you can take it away if it looks scary to you. 只需要使用axis.args参数才能使图例看起来不错,如果对您来说吓人,则可以将其axis.args

这个

Source: 资源:

https://gis.stackexchange.com/questions/17339/raster-legend-in-r-how-to-colour-specific-values https://gis.stackexchange.com/questions/17339/raster-legend-in-r-how-to-colour-specific-values

should solve your problem. 应该可以解决您的问题。

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

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