Skip to content
Prev 156301 / 398506 Next

Creating smooth color regions with panel.contourplot()

On 9/15/08, Waichler, Scott R <Scott.Waichler at pnl.gov> wrote:
Manually creating polygons with contourLines will not really help
because (1) as you noted, there will be holes, and (2) contours that
cross edges will be open. The only real solution is to color each
rectangle individually, and that would be very inefficient in R code
(grid does not have a C-level interface).

The good news is that filled.contour() already does this efficiently,
and you can use that through the gridBase package:


panel.filledcontour <-
    function(x, y, z, subscripts,
             at,
             col.regions = cm.colors,
             col = col.regions(length(at) - 1),
             ...)
{
    stopifnot(require("gridBase"))
    z <- matrix(z[subscripts],
                nrow = length(unique(x[subscripts])),
                ncol = length(unique(y[subscripts])))
    if (!is.double(z)) storage.mode(z) <- "double"
    opar <- par(no.readonly = TRUE)
    on.exit(par(opar))
    if (panel.number() > 1) par(new = TRUE)
    par(fig = gridFIG(), omi = c(0, 0, 0, 0), mai = c(0, 0, 0, 0))
    cpl <- current.panel.limits()
    plot.window(xlim = cpl$xlim, ylim = cpl$ylim,
                log = "", xaxs = "i", yaxs = "i")
    .Internal(filledcontour(as.double(do.breaks(cpl$xlim, nrow(z) - 1)),
                            as.double(do.breaks(cpl$ylim, ncol(z) - 1)),
                            z, as.double(at), col = col))
}

plot.new()

levelplot(volcano, panel = panel.filledcontour,
          col.regions = terrain.colors,
          cuts = 25)

-Deepayan