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

I want to change the 'Brown Green' theme so that the middle break (140 to 160) is gray. Is that possible?

Here's an example with the volcano data set.

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

www
  • 38,575
  • 12
  • 48
  • 84
derelict
  • 3,657
  • 3
  • 24
  • 29

2 Answers2

4

We can prepare a color palette first and replace the third one to be grey, and then put it to the region argument.

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

www
  • 38,575
  • 12
  • 48
  • 84
3

This may be irrelevant but by manually defining the breaks you are missing parts of the data around the minimum (where you are getting white patches). Below is a way to get around this issue:

#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

Majid
  • 1,836
  • 9
  • 19