Skip to content
Prev 25674 / 29559 Next

Specify color for "zero" raster values using levelplot

Dear all,

I am trying to change the color for zero values in a map produced using levelplot to plot a raster file. Specifically, I want to reproduce this figure: http://i.imgur.com/mjXxZhO.png, where a red to blue scale is used, but notice that zero values have been replaced by grey.

As an example, let's use an adapted version of the August irradiation code from the rasterVis webpage:

################################
library(raster)
library(ncdf4)
library(rasterVis)

##Solar irradiation data from CMSAF 
old <- setwd(tempdir())
download.file('https://raw.github.com/oscarperpinan/spacetime-vis/master/data/SISmm2008_CMSAF.zip',
'SISmm2008_CMSAF.zip', method='wget')
unzip('SISmm2008_CMSAF.zip')

listFich <- dir(pattern='\\.nc')
stackSIS <- stack(listFich)
stackSIS <- stackSIS * 24 ##from irradiance (W/m2) to irradiation Wh/m2
idx <- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')

SISmm <- setZ(stackSIS, idx)
names(SISmm) <- month.abb

setwd(old)

# Set color palette
myTheme=rasterTheme(region=brewer.pal('RdBu', n=11))

Aug <- raster(SISmm, 8)
meanAug <- cellStats(Aug, mean)
levelplot(Aug - meanAug, par.settings = myTheme, margin=FALSE)

################################

In the example above, how can I replace the color of "zero values" with grey?
 
Thanks,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota