Hi


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,
               key.axes=customAxis())


Hope that helps.


Paul
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
[EMAIL PROTECTED]
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to