Levelplot 颜色键 - 范围和极值

Levelplot color key - range and extremes

是否可以在 R 中创建如下所示的色键? (这个来自软件Grid Analysis and Display System - Grads)。

有两个功能我无法在 R 中重现:

  1. 序列是非线性的,但它显示为好像
  2. 大于200的值为灰色/小于0的值为白色

我正在使用 rastervis 的 levelplot,它使用格子 levelplot 绘制栅格:

require(raster)
require(rasterVis)

set.seed(200)

X = seq(-40,0,by=1)
Y = seq(-60,-40,by=1)
grid = expand.grid(list(X=X,Y=Y))
Z = rnorm(nrow(grid),mean=10,sd=100)

data = data.frame(grid,Z)
r = rasterFromXYZ(data)
mapTheme <- rasterTheme(region=c('#EEF7FA','#D6F8F7',"#BEDAFF",'#5DA4FF',"#0000FF","#D4F9E2","#00FF7F","#008000","#FFFF00","#FFD27F", "#FFB732"    ,"#EE7600",
                                 "#D53E4F","#FF6A6A"))  
my.at = c(0,1,5,10,15,20,25,30,40,50,75,100,150,200)
myColorkey <- list(at=my.at,
                   space="bottom",
                   labels=list(at=my.at))
p=levelplot(r, par.settings=mapTheme,at = my.at, colorkey=myColorkey,margin=F)
print(p)

结果:

如您所见,小于0和大于200的值都是白色,我不知道如何设置大于或小于某个值的值显示为特定颜色。另外,如何使颜色键中连续粗标记之间的 space 具有相同的大小,尽管间隔不相同?

这是非线性序列大小相等间隔的解决方法:

library(raster)
library(rasterVis)

set.seed(200)
X = seq(-40,0,by=1)
Y = seq(-60,-40,by=1)
grid = expand.grid(list(X=X,Y=Y))
Z = rnorm(nrow(grid),mean=10,sd=100)

data = data.frame(grid,Z)
r = rasterFromXYZ(data)
mapTheme <- rasterTheme(region=c('#EEF7FA','#D6F8F7',"#BEDAFF",'#5DA4FF',"#0000FF","#D4F9E2","#00FF7F",
                                 "#008000","#FFFF00","#FFD27F", "#FFB732" ,"#EE7600", "#D53E4F","#FF6A6A"))  

my.at=c(0,1,5,10,15,20,25,30,40,50,75,100,150,200)
my.brks=seq(0, 200, by=15)

myColorkey <- list(at=my.brks, labels=list(at=my.brks, labels=my.at), space="bottom")
p=levelplot(r, par.settings=mapTheme, at=my.at, colorkey=myColorkey, margin=F)
print(p)

对于小于 0 和大于 200 的值,这可能是一个解决方案:

library(raster)
library(rasterVis)

set.seed(200)
X = seq(-40,0,by=1)
Y = seq(-60,-40,by=1)
grid = expand.grid(list(X=X,Y=Y))
Z = rnorm(nrow(grid),mean=10,sd=100)

data = data.frame(grid,Z)
r = rasterFromXYZ(data)
mapTheme <- rasterTheme(region=c('white','#EEF7FA','#D6F8F7',"#BEDAFF",'#5DA4FF',"#0000FF","#D4F9E2","#00FF7F",
                                 "#008000","#FFFF00","#FFD27F", "#FFB732" ,"#EE7600", "#D53E4F","#FF6A6A", "gray"))  
max(values(r))
min(values(r))

my.at=c(min(values(r)), 0,1,5,10,15,20,25,30,40,50,75,100,150,200, max(values(r)))
my.brks=seq(0, 200, by=13)

myColorkey <- list(at=my.brks, labels=list(at=my.brks, labels=c(-276,0,1,5,10,15,20,25,30,40,50,75,100,150,200, 388)), space="bottom")
p=levelplot(r, par.settings=mapTheme, at=my.at, colorkey=myColorkey, margin=F)
print(p)

您的颜色没有从浅变深。您可以使用 RColorBrewer 包来解决这个问题。

library(RColorBrewer)
reds = brewer.pal(5, "YlOrRd")
greens = brewer.pal(3, "Greens")
blues = brewer.pal(5, "Blues")
mapTheme <- rasterTheme(region=c('white', blues, greens, reds, "gray"))

这是一个非常有用的解决方法。虽然没有解决问题 1,但我发现对问题 2 有用的东西(为值 below/above 添加三角形,颜色条范围的限制)可以通过添加以下内容来实现:

library(s2dverification)
data_array <- array(Z, dim = c(length(X), length(Y)))
PlotEquiMap(data_array, X, Y,bar_limits=c(0,200),col_inf='white',col_sup='gray')

raster with colorbar

更新格子的另一种解决方案:

library(raster)
library(rasterVis)

set.seed(200)
X = seq(-40,0,by=1)
Y = seq(-60,-40,by=1)
grid = expand.grid(list(X=X,Y=Y))
Z = rnorm(nrow(grid),mean=10,sd=100)
data = data.frame(grid,Z)
r = rasterFromXYZ(data)

levelplot(r, margin=F, at=c(-Inf, seq(0, 200, 20), Inf),
          colorkey = list(tri.lower = TRUE, tri.upper = TRUE))

只要您在 at 定义中添加“-Inf”和“Inf”,就会激活向颜色栏添加三角形的选项。