修改现有的R Color Brewer调色板

时间:2019-02-23 21:33:08

标签: r raster r-raster rastervis

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

我想更改“棕绿色”主题,以使中间中断(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)

raster plot

2 个答案:

答案 0 :(得分:3)

我们可以先准备一个调色板,然后将第三个调色板替换为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)

enter image description here

答案 1 :(得分:2)

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

#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)

enter image description here