[R] color palette from red to blue passing white

hadley wickham h.wickham at gmail.com
Tue Dec 4 19:26:29 CET 2007


> Hmm,  I would have recommended
>
>   colorRampPalette(c('dark red','white','dark blue'),
>                    space = "Lab")
>
> where the 'space = "Lab"' part also makes sure that a
> "perceptually-based" space rather than RGB is used.
>
> I think the functions colorRamp() and (even more)
> colorRampPalette()  are very nice, part of "standard R" and
> still not known and used enough.

Well I use them in ggplot2 :)  Unfortunately I've noticed that space =
"Lab" is much slower than space = "RGB" which is why I don't use Lab
space as a default.  And it also ignores alpha values, which is a bit
of a pain.

> Note that they are based on 'convertColor()' and other color
> space functionality in R all of which deserve more usage
> in my oppinion and also in my own code ! ;-)

I find colour manipulation generally painful - all the really hard
stuff is there (i.e. conversion between colour spaces), but convenient
functions are lacking.   For example, I have this alpha function in
ggplot:

alpha <- function(colour, alpha) {
  col <- col2rgb(colour, TRUE) / 255
  col[4, ] <- rep(alpha, length(colour))

  new_col <- rgb(col[1,], col[2,], col[3,], col[4,])
  new_col[is.na(colour)] <- NA
  new_col
}

which seems like a lot of work for a simple task.  The fact that
col2rgb and rgb aren't symmetric is frustrating, especially since one
outputs values between 0 and 255 and the other (by default) accepts
inputs between 0 and 1.

> Package 'vcd' (and others) use package 'colorspace',
> and I have wondered in the past if these color space computations
> should not be merged into to standard R (package 'grDevices').
> But that's really a topic for another thread, on R-devel, not R-help..

It would definitely be nice if all colour space manipulations were
merged into a single package, with a consistent interface.  I would
happily contribute to such a project, since I do a lot of colour
manipulation in ggplot.

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list