[R] color of opposite sign values in filled.contour

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jan 5 15:46:00 CET 2007

On 1/5/2007 9:08 AM, Claudia Tebaldi wrote:
> Dear all,
> especially those of you that kindly provided suggestions yesterday,
> I was not asking for cool palettes -- even if I now appreciate the
> pointers -- but I was asking for a way to make the 0 level of a filled
> contour plot correspond to the neutral color in the color scale
> when the range of my values is not symmetrical around zero...without
> hacking into the filled.contour function (which I'm not able to do
> succesfully). I couldn't get the ggplot package suggested because I'm not
> running MAC OSX 10.4, unfortunately...all the other suggestions didn't
> provide a way out...at least that I could recognize.
> Thanks again -- last time I bother you I promise!

The easiest way would be to use the "levels" parameter to create 
breakpoints which aren't linear in the original scale.  For example,

x <- y <- seq(-10,10,len=20)
z <- outer(x, y, function(x,y) x^2+y^2)
filled.contour(x,y,z, levels=seq(0,15)^2)

If you don't want to transform the spacing between the colors, you just 
want to change the sequence you get, then you should write your own 
palette function.  For example,

mypalette <- function(n) {
   n1 <- n %/% 3
   n2 <- n - n1 + 1
   c( colorRampPalette(c("red", "white"))(n1),
      colorRampPalette(c("white", "blue"))(n2)[-1] )

filled.contour(x,y,z, color=mypalette)

Duncan Murdoch

More information about the R-help mailing list