Waichler, Scott R
2008-Sep-15 21:07 UTC
[R] Creating smooth color regions with panel.contourplot()
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). Thanks, Scott Waichler Pacific Northwest National Laboratory scott.waichler at pnl.gov
Deepayan Sarkar
2008-Sep-17 17:51 UTC
[R] Creating smooth color regions with panel.contourplot()
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
David Carslaw
2008-Sep-17 20:12 UTC
[R] Creating smooth color regions with panel.contourplot()
I think this is a very useful function that I imagine has wide appeal - thanks. Using the code below produces the plot OK but when I try and copy/save it (as a metafile) I receive the following error and an hourglass: Error: invalid graphics state [using XP, 2.72, lattice 0.17.13] Regards, David Deepayan Sarkar wrote:> > 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >----- Institute for Transport Studies University of Leeds -- View this message in context: http://www.nabble.com/Creating-smooth-color-regions-with-panel.contourplot%28%29-tp19501169p19540180.html Sent from the R help mailing list archive at Nabble.com.