From Richard.Cotton at hsl.gov.uk  Thu May 21 18:28:22 2009
From: Richard.Cotton at hsl.gov.uk (Richard.Cotton at hsl.gov.uk)
Date: Thu, 21 May 2009 17:28:22 +0100
Subject: [Rd] Colour Schemes
In-Reply-To: <d8ad40b50905210712k128ecfa2vff2dd178f946680d@mail.gmail.com>
Message-ID: <OF8F030E35.855338DD-ON802575BD.00574782-802575BD.005A7CF2@hsl.gov.uk>

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 
colour
> > maps outside of the plotting functions, if only so that the code can 
be
> > 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 
colour") 
   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))

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

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



[Rd] Colour Schemes
Richard.Cotton at hsl.gov.uk