修改现有的 R Color Brewer 调色板

Modify existing R Color Brewer palette

library(raster); library(rasterVis); library(RColorBrewer)

我想更改 'Brown Green' 主题,使中间分隔符(140 到 160)为灰色。 那可能吗?

下面是火山数据集的示例。

breaks <- c(100, 120, 140, 160, 180, 195) # manual breaks
mapTheme <- rasterTheme(region=brewer.pal(6,"BrBG"))
levelplot(volcano, at=breaks, par.settings=mapTheme)

我们可以先准备一个调色板,把第三个替换成grey,然后放到region参数中。

library(raster)
library(rasterVis)
library(RColorBrewer)

breaks <- c(100, 120, 140, 160, 180, 195) # manual breaks
pal <- brewer.pal(6,"BrBG")
pal[3] <- "grey"
mapTheme <- rasterTheme(region = pal)
levelplot(volcano, at=breaks, par.settings=mapTheme)

这可能无关紧要,但通过手动定义中断,您会丢失最小值附近的部分数据(您会得到白色补丁)。以下是解决此问题的方法:

#setting breaks
myMat.max <- ceiling(max(volcano))
myMat.min <- floor(min(volcano))
breaks <- round(seq(myMat.min, myMat.max, length.out = 6)) # do not use by = x

# Customising Brewer palette
pal <- brewer.pal(6,"BrBG")
pal[3] <- "grey"
mapTheme <- rasterTheme(region = pal)

#plot
levelplot(volcano, colorkey=list(at=breaks, labels=as.character(breaks)), 
          par.settings=mapTheme, cuts=length(breaks)-2)