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

Don McKenzie dmck at u.washington.edu
Tue Nov 26 04:28:48 CET 2013


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:

> Bert or anyone else familiar with RColorBrewer:
> 
> Has anyone tried to accomplish with RColorBrewer what I asked about in my original post (below)? 
> 
> Here is an example cribbed from the levelplot() help examples
> 
> x <- seq(pi/4, 5 * pi, length.out = 100)
> y <- seq(pi/4, 5 * pi, length.out = 100)
> r <- as.vector(sqrt(outer(x^2, y^2, "+")))
> grid <- expand.grid(x=x, y=y)
> grid$z <- cos(r^2) * exp(-r/(pi^3))
> 
> # now use RColorBrewer to get a palette
> 
> library("RColorBrewer”)
> levelplot(z~x*y, grid,col.regions=brewer.pal(6,"BrBG”))   # the numeric argument to brewer.pal is the number of colors used — I tried several
> 
> This gives me a nice brown-to-green gradient but does not (AFAICS) give me control over where the center of the divergence lies. Even in this symmetrical
> example, I can’t get it to be at zero — it repeats on either side of zero.
> 
> thanks to anyone who pages through all this and makes a suggestion, even if it doesn’t work.  :-)
> 
> On Nov 22, 2013, at 10:25 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> 
>> Use the Rcolorbrewer package.
>> 
>> -- Bert
>> 
>> On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <dmck at u.washington.edu> wrote:
>>> I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
>>> positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
>>> is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).
>>> 
>>> I am using R 3.0.2 on OSX 10.9
>>> 
>>> Here is an example
>>> 
>>> x <- y <- c(1:25)
>>> grid <- expand.grid(x=x,y=y)
>>> grid$z <- sort(runif(625,min=-1,max=12))
>>> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5
>>> 
>>> # do something clever here
>>> # e.g., my.colors <- <create a palette that splits at zero>
>>> 
>>> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue
>>> 
>>> Ideas appreciated.  Thanks in advance.
>>> 
>>> 
>> 
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>> 
>> (650) 467-7374
> 
> 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
> 
> 
> 

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



More information about the R-help mailing list