Skip to content
Prev 333676 / 398506 Next

specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

In case anyone cares (?), here is a function to do what I was asking, which doesn?t use colorBrewer but could with some hacking. I?m sure it?s fragile, but it works with well behaved integers and zero in the middle, which was all I needed. The output is a palette that can be passed to levelplot() and other functions.

Cheers

diverge.color <- function(start.color,end.color,min.value,max.value,mid.value=0,mid.color="ivory")

{
	# based on ideas from Maureen Kennedy, Nick Povak, and Alina Cansler

        # creates a palette for the current session for a divergent-color
	# graphic with a non-symmetric range
	# "cuts" = the number of slices to be made in the range above and below "mid.value"

        ramp1 <- colorRampPalette(c(start.color,mid.color))
        ramp2 <- colorRampPalette(c(mid.color,end.color))

       # now specify the number of values on either side of "mid.value"
       
       max.breaks <- round(max.value - mid.value)
       min.breaks <- round(mid.value - min.value)
       
       num.breaks <- max(max.breaks,min.breaks)
       
       low.ramp <- ramp1(num.breaks)
       high.ramp <- ramp2(num.breaks)
       
       # now create a combined ramp from the higher values of "low.ramp" and 
       # the lower values of "high.ramp", with the longer one using all values 
       # high.ramp starts at 2 to avoid duplicating zero
       
       myColors <- c(low.ramp[(num.breaks-min.breaks):num.breaks],high.ramp[2:max.breaks])
  
      myColors
}
On Nov 25, 2013, at 4:27 PM, Don McKenzie <dmck at u.washington.edu> wrote:

            
Don McKenzie
Research Ecologist
Pacific Wildland Fire Science Lab
US Forest Service

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
dmck at uw.edu