Skip to content
Prev 13518 / 29559 Next

setting breaks in colorramps

On Fri, Nov 25, 2011 at 1:35 PM, PepET <pep.bioalerts at gmail.com> wrote:
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:
[1] "#000000FF"  # black
[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