[R-sig-Geo] setting breaks in colorramps

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Nov 25 18:47:23 CET 2011


On Fri, Nov 25, 2011 at 1:35 PM, PepET <pep.bioalerts at gmail.com> wrote:
> Hey Tim,
>
> Thank you for you advice...it is indeed an interesting funciton, however, it
> set the steps between colors, but does not allow to assign a certain color
> with a certain value in your raster....or maybe I am just not
> understanding...

 Its a common problem in R - the standard way of colouring things
isn't a mapping of values to colours, but a mapping of values to
indexes in a palette. Getting that palette index to be the right
colour is tricky.

 I had a go at making something that would help with my colourschemes
package (on R-forge) which lets you define function closures that map
values to colours given some scheme. So for example you can do:

tramp = multiRamp(rbind(c(-2000,0),c(0,1000),c(1000,9000)),
+       list(c("black","blue"),c("green","brown"),c("gray70","gray70"))
+       )

Then that gives you 'tramp' - a function that will map any value to
the colour in the ramps specified. So:

> tramp(-2000)
[1] "#000000FF"  # black
> tramp(500)
[1] "#539515FF"  # between green and brown

And then if you are doing several plots with different vectors and
want to make sure they use the sample colour scheme, you do:

plot(x1,y1, col = tramp(v1))

plot(x2,y2, col=tramp(v2))

- wherever v1 or v2 is 500 the point will be the same greeny-brown
colour. Guaranteed.

BUT there's not an easy way to hook this kind of behaviour into image
and raster plots... The kludge I've used to image an n*m matrix, V,
say, is to create a matrix of integers 1:(n*m) called IV, and then a
vector of colours tramp(V) (which will be length n*m). Then do
image(IV,col=IV). A bit wasteful but gets the job done. More info in
the vignette for colourschemes.

Barry



More information about the R-sig-Geo mailing list