[R] filled.contour

Paul Murrell p.murrell at auckland.ac.nz
Mon Apr 19 00:41:52 CEST 2004


jzhang10 wrote:
> Hi,
> I want to draw a level plot. The levels are not evenly spaced, so I did 
> something like: levels=c(0,2,5,10,30,60). I still want the color bar (key) on 
> the right side to be evenly spaced so that the small numbers (0,2,5) are not 
> squeezed together.
> Does anyone know how to do it?

One way is to use the key.axes argument to draw the key yourself.
Below is a simple example;  this makes use of the fact that the key is 
just another plot with a scale of c(0, 1) on the x-axis and a scale of 
c(min(levels), max(levels)) on the y-axis.

x <- y <- seq(-4*pi, 4*pi, len = 27)
r <- sqrt(outer(x^2, y^2, "+"))
# Range of r is 0 to 17.77153
levels <- c(0, 2, 4, 8, 18)
cols <- cm.colors(length(levels) - 1)

# Default key has levels on linear axis
# Plot looks awful, but that's not the point ...
filled.contour(cos(r^2)*exp(-r/(2*pi)), levels=levels, col=cols)

# Custom axis producing even spacing of levels
customAxis <- function() {
   n <- length(levels)
   # Generate even spacing over y-scale range
   y <- seq(min(levels), max(levels), length.out=n)
   # Draw main key with rectangles
   # (using same colours as in main plot)
   rect(0, y[1:(n-1)], 1, y[2:n], col=cols)
   # Draw key axis
   # NOTE: labels do not correspond to "real" y-locations
   axis(4, at=y, labels=levels)
filled.contour(cos(r^2)*exp(-r/(2*pi)), levels=levels, col=cols,

Hope that helps.

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz

More information about the R-help mailing list