[R] Creating smooth color regions with panel.contourplot()

Waichler, Scott R Scott.Waichler at pnl.gov
Thu Sep 18 22:54:48 CEST 2008


> Does the contour() solution work? If not, there's not really 
> much that can be done.

Yes, I figured out how to add the naming of arguments.  This works
great:

library(gridBase)
library(lattice)
data(volcano)

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")
  # paint the color contour regions
  .Internal(filledcontour(as.double(do.breaks(cpl$xlim, nrow(z) - 1)),
                          as.double(do.breaks(cpl$ylim, ncol(z) - 1)),
                          z, levels = as.double(at), col = col))
  # add contour lines
  contour(as.double(do.breaks(cpl$xlim, nrow(z) - 1)),
          as.double(do.breaks(cpl$ylim, ncol(z) - 1)),
          z, levels = as.double(at), add=T,
          col = "gray", # color of the lines
          drawlabels=F  # add labels or not
         )
}

pdf("volcano.pdf")
plot.new()

print(levelplot(volcano, panel = panel.filledcontour,
          col.regions = terrain.colors,
          cuts = 10,
          plot.args = list(newpage = FALSE)))
dev.off()

--Scott Waichler



More information about the R-help mailing list