简体   繁体   English

Plot 光栅,具有连续调色板,白色为零(R 基)

[英]Plot raster with continuous color palette with zero in white (R Base)

As much as I looked at other questions I couldn't solve my problem (I'm new in R).尽管我查看了其他问题,但我无法解决我的问题(我是 R 新手)。 I simply need to plot a raster where the minimum value (let's say color red) goes to zero (white) and from zero to maximum (color blue) continuously.我只需要 plot 一个栅格,其中最小值(比如说红色)连续变为零(白色)和从零到最大值(蓝色)。 I would like to create that color palette independently if the data is symmetrically distributed in negative and positive values.如果数据以负值和正值对称分布,我想独立创建该调色板。

Let's say I have a raster with this values:假设我有一个具有以下值的栅格:

library(raster)
values <- c(seq(-2000,0,by=1),seq(1,499,by=1))
values <- sample(values)

r <- raster(ncol=50,nrow=50)
r <- setValues(r,values)

plot(r)

If this has already been resolved in another question, I would appreciate any information.如果这已经在另一个问题中得到解决,我将不胜感激任何信息。 Thank you谢谢

So, you can use RColorBrewer or colorRampPalette to achieve this (or a combination of both) and by setting breaks .因此,您可以使用RColorBrewercolorRampPalette来实现这一点(或两者的组合)并通过设置breaks

library(raster)
library(RColorBrewer)

breakpoints <- c(-2000, seq(0, 500, 55.6))
colors <- c("red", RColorBrewer::brewer.pal(9, "Blues"))
plot(r, breaks = breakpoints, col = colors)

Output Output 在此处输入图像描述

You can also do something similar with colorRampPalette by setting two unique colors.您也可以通过设置两个唯一的 colors 来使用 colorRampPalette 执行类似的操作。 Then, in parenthesis, define how many colors on the gradient you want.然后,在括号中,定义你想要的渐变上有多少 colors。

colors <- c("red", colorRampPalette(c("steelblue1", "steelblue4"))(9))

You can also use these both in conjunction with one another.您也可以将这两者结合使用。

colors <- c("red", colorRampPalette(RColorBrewer::brewer.pal(9, "Blues"))(11))

If you want red also continuous, then you could create a gradient for both.如果你想让红色也连续,那么你可以为两者创建一个渐变。

breakpoints <- c(seq(-2000, -1, 222), seq(0, 500, 55.6))
colors <- c(RColorBrewer::brewer.pal(9, "Reds"), RColorBrewer::brewer.pal(9, "Blues"))
plot(r, breaks = breakpoints, col = colors)

Output Output 在此处输入图像描述

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

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