Skip to content

color palette from red to blue passing white

8 messages · Linda Smith, jim holtman, Achim Zeileis +5 more

#
see if this is what you need:

require(lattice)
x <- matrix(1:100,10)
levelplot(x,col.regions=colorRampPalette(c('dark red','white','dark blue')))
On Dec 3, 2007 5:41 PM, Linda Smith <lsmithingm at gmail.com> wrote:

  
    
#
On Mon, 3 Dec 2007, jim holtman wrote:

            
Instead of colorRampPalette(), you could also use diverge_hcl() in package
"vcd" to get a perceptually-based version, e.g.,

  levelplot(x, col.regions = diverg_hcl(16))
#

        
AZ> On Mon, 3 Dec 2007, jim holtman wrote:
>> see if this is what you need:
    >> 
    >> require(lattice) x <- matrix(1:100,10)
    >> levelplot(x,col.regions=colorRampPalette(c('dark
    >> red','white','dark blue')))

    AZ> Instead of colorRampPalette(), you could also use
    AZ> diverge_hcl() in package "vcd" to get a
    AZ> perceptually-based version, e.g.,

    AZ>   levelplot(x, col.regions = diverg_hcl(16))

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.
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 ! ;-) 

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..

Martin Maechler, ETH Zurich


    >> On Dec 3, 2007 5:41 PM, Linda Smith
    >> <lsmithingm at gmail.com> wrote: > Hi All,
    >> >
    >> > I am looking for a color palette like this: >
    >> http://www.ncl.ucar.edu/Applications/Images/h_long_5_lg.png
    >> >
    >> > I think I found out how some time ago (something like
    >> Colors[1:n]), but when > I now wanna use it, I could not
    >> remember how I did it.
    >> >
    >> > Does anyone know which package I could use?
    >> >
    >> > Many thanks!
    >> >
    >> > Linda
#
Linda Smith wrote:
Hi Linda,
You can do it with color.legend in plotrix.

plot(1:10)
color.legend(1,8,7,8.5,align="rb",
rect.col=color.scale(1:21,c(0,1,0.5),c(0,1,0),c(0.5,1,0)),
legend=seq(-50,50,by=10))

Jim
#
On Tue, 4 Dec 2007, Martin Maechler wrote:
At one time, the fact that colorspace used S4 classes was a problem for 
including it in base R.

 	-thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle
#
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.
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.
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
#
hadley wickham <h.wickham <at> gmail.com> writes:
<<<pruned>>>
It's a good thought.
There is some thought going into standardizing, if 
you look at

http://developer.R-project.org/sRGB-RFC.html

and by the way, for a previous post, it's inaccurate to call Lab
"perceptual", although many people do.  It's based on discrimination data
in an attempt to make a uniform space for small color differences, 
but it is only approximately so.

best,

Ken

PS, apologies that gmane is making me prune some quoted text