[R] Another plot question

Paul Murrell p.murrell at auckland.ac.nz
Fri Nov 29 03:54:24 CET 2002


Hi


Andy Bunn wrote:
> 
> Here is another, perhaps trivial, plotting question.
> 
> I am making a filled contour plot and have almost everything dialed in
> the way I want it. The last hurdle (that I can foresee) is adjusting the
> look of the key. I'm plotting the 25%, 50%, 75%, and 95% quantiles and I
> want the key to be evenly spaced, e.g., the width of the 25% and the 95%
> should look the same on the key. They are labeled and ticked right. Is
> there a way to space them correctly?


I think you have to hack filled.contour slightly to get what you want. 
It's not a major hack;  I've attached a hacked filled.contour -- look
for the comment ## HACK -- which together with the slight modification
of your example below gives what I think you are looking for ...

    junk.mat <- matrix(rnorm(12800), 128, 100)
    xYears <- 0:(nrow(junk.mat)-1)
    yPeriod <- 1:ncol(junk.mat)
    temp <- seq(10, ncol(junk.mat), 10)
    myLabels <- 2^(temp/10)
    cwtquants <- quantile(junk.mat,
                          probs = c(0, 0.25, 0.5, 0.75, 0.95, 1))
    quant2plot <- c(cwtquants[1], cwtquants[2],
                    cwtquants[3], cwtquants[4],
    cwtquants[5], cwtquants[6])
    # Need hacked filled.contour (in attached file)
    source("myfilled.contour.R")
    filled.contour(xYears, 
                   yPeriod,
                   junk.mat,
                   levels = quant2plot,
                   col=rainbow(5),
                   plot.axes = {axis(1); 
                                axis(2, temp,
                                     labels = myLabels[10:1]) },
                   plot.title =
                     title(main = "Continuous Wavelet Transform",
                           xlab = "Time(Years)",
                           ylab = "Log(Period)"),
                   key.title = title(main="Power"),
                   # key.axes = axis(4, quant2plot))
                   key.axes = axis(4,
                     at=1:length(quant2plot),
                     labels=quant2plot))
    
Hope that helps

Paul
-------------- next part --------------
filled.contour <-
function (x = seq(0, 1, len = nrow(z)),
          y = seq(0, 1, len = ncol(z)),
          z,
          xlim = range(x, finite=TRUE),
          ylim = range(y, finite=TRUE),
          zlim = range(z, finite=TRUE),
          levels = pretty(zlim, nlevels), nlevels = 20,
          color.palette = cm.colors,
          col = color.palette(length(levels) - 1),
          plot.title, plot.axes, key.title, key.axes,
          asp = NA, xaxs="i", yaxs="i", las = 1, axes = TRUE, ...)
{
    if (missing(z)) {
        if (!missing(x)) {
            if (is.list(x)) {
                z <- x$z
                y <- x$y
                x <- x$x
            }
            else {
                z <- x
                x <- seq(0, 1, len = nrow(z))
            }
        }
        else stop("no `z' matrix specified")
    }
    else if (is.list(x)) {
        y <- x$y
        x <- x$x
    }
    if (any(diff(x) <= 0) || any(diff(y) <= 0))
        stop("increasing x and y values expected")

    mar.orig <- (par.orig <- par(c("mar","las","mfrow")))$mar
    on.exit(par(par.orig))

    w <- (3 + mar.orig[2]) * par('csi') * 2.54
    layout(matrix(c(2, 1), nc=2), widths=c(1, lcm(w)))
    par(las = las)

    ## Plot the `plot key' (scale):
    mar <- mar.orig
    mar[4] <- mar[2]
    mar[2] <- 1
    par(mar = mar)
    plot.new()
    ## HACK
    # plot.window(xlim=c(0,1), ylim=range(levels), xaxs="i", yaxs="i")
    # rect(0, levels[-length(levels)], 1, levels[-1], col = col)
    nlevels <- length(levels)
    plot.window(xlim=c(0,1), ylim=c(1, nlevels), xaxs="i", yaxs="i")
    rect(0, 1:(nlevels-1), 1, 2:nlevels, col = col)
    # end hack
    if (missing(key.axes)) {
        if (axes)
            axis(4)
    }
    else key.axes
    box()
    if (!missing(key.title))
	key.title

    ## Plot contour-image::
    mar <- mar.orig
    mar[4] <- 1
    par(mar=mar)
    plot.new()
    plot.window(xlim, ylim, "", xaxs=xaxs, yaxs=yaxs, asp=asp)

    if (!is.matrix(z) || nrow(z) <= 1 || ncol(z) <= 1)
        stop("no proper `z' matrix specified")
    if (!is.double(z))
        storage.mode(z) <- "double"
    .Internal(filledcontour(as.double(x),
                            as.double(y),
                            z,
                            as.double(levels),
                            col = col))
    if (missing(plot.axes)) {
        if (axes) {
            title(main="", xlab="", ylab="")
            axis(1)
            axis(2)
        }
    }
    else plot.axes
    box()
    if (missing(plot.title))
        title(...)
    else
	plot.title
    invisible()
}


More information about the R-help mailing list