[Rd] Colour Schemes

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Thu May 21 18:28:22 CEST 2009

I'm going to take your second example first.

>  The base graphics "image" function has zlim arguments which let you do:
>  z=outer(1:10,1:10,"*")
>  image(z)
>  image(z/2, zlim=range(z))
>  but again, not obvious, and complex/impossible when using more
> sophisticated colour mappings.

The way to do more complex examples, similar to the manner you suggested 
originally, is use the breaks and col arguments, e.g.

breaks <- c(0,25,75,100)
col <- c("red", "blue", "green")
image(z, breaks=breaks, col=col)
image(z/2, breaks=breaks, col=col)

So it is possible here, though perhaps not obvious.

With your first example ...
>  The problem here is that the user doesn't have exact control of the
> mapping from value to colour. For example (using a slightly more
> safe-for-use-after-lunch version of the levelplot example grid):
>      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 <- r
>      grid$z2 = r *0.5
> Then I do:
>   levelplot(z~x*y, grid, cuts = 5, col.regions=rainbow(5))
>  very nice, but suppose I want to show $r2 on the same colour scale, I
> can't just do:
>  levelplot(z2~x*y, grid, cuts = 5, col.regions=rainbow(5))
>  because that looks the same as the first one since levelplot uses the
> whole colour range.

... I agree that the inability to specify a vector of cut points ruins 
your chances of doing what you want.

> > There may be some utility in creating functions to generate these 
> > maps outside of the plotting functions, if only so that the code can 
> > recycled for new functions.
>  Exactly, it would make a new package.

Excellent.  I reckon keeping the cut vector/colour vector input format 
would be sensible, e.g.

continuousColours <- function(x, breaks, col)
   if(length(breaks) != length(col)+1) stop("must have one more break than 
   col[as.numeric(cut(x, breaks))]

x <- runif(10, 0, 5)
breaks <- 0:5
col <- c("red", "blue", "green", "cyan", "magenta")
plot(1:10, x, col=continuousColours(x, breaks, col))


Mathematical Sciences Unit


This message contains privileged and confidential inform...{{dropped:20}}

More information about the R-devel mailing list