[R] levelplot and unequal cell sizes

Waichler, Scott R Scott.Waichler at pnl.gov
Fri Apr 27 00:46:47 CEST 2007


> my.panel.levelplot <-
>     function (x, y, z, subscripts, at = pretty(z),
>               col.regions = regions$col, ...,
>               w, h)
> {
>     regions <- trellis.par.get("regions")
>     numcol <- length(at) - 1
>     numcol.r <- length(col.regions)
>     col.regions <- if (numcol.r <= numcol)
>         rep(col.regions, length = numcol)
>     else col.regions[floor(1+(1:numcol-1) * (numcol.r-1)/(numcol-1))]
>     zcol <- findInterval(z, at, rightmost.closed = TRUE)
>     x <- as.numeric(x[subscripts])
>     y <- as.numeric(y[subscripts])
>     z <- as.numeric(z[subscripts])
>     w <- as.numeric(w[subscripts])
>     h <- as.numeric(h[subscripts])
>     zcol <- as.numeric(zcol[subscripts])
>     print(data.frame(z, x.node, y.node, w.node, h.node, 
> col.regions[zcol]))
>     panel.rect(x = x, y = y, width = w, height = h,
>                col = col.regions[zcol], ...) }
> 

Another note on this panel function to use with levelplot()---the cut()
function may be better than findInterval() for applications where you
want NA returned for values outside the supplied interval ranges.  So
intead of using 
  zcol <- findInterval(z, at, rightmost.closed = TRUE)
above, I use
  zcol <- as.integer(cut(z, at, labels=F))
Intervals here are closed on the right, open on the left (b1, bd].  So,
for at = (1,2,3) and z = (1,1.01,3,3.01), then zcol = (NA,1,2,NA).  I
represent NA with "transparent" in the final color contour plot.  

Scott Waichler



More information about the R-help mailing list