[R] Creating smooth color regions with panel.contourplot()
Deepayan Sarkar
deepayan.sarkar at gmail.com
Wed Sep 17 19:51:29 CEST 2008
On 9/15/08, Waichler, Scott R <Scott.Waichler at pnl.gov> wrote:
> When I use panel.contourplot() with filled color regions, the coloring
> follows the stair-step edge of the underlying grid instead the smooth
> contour lines themselves. How can I get the latter behavior? I would
> guess there is a much simpler way than manually creating polygons with
> contourLines(), especially since a contour interval/region can have
> holes inside it (different contour intervals).
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
More information about the R-help
mailing list