s-news
[Top] [All Lists]

modification to levelplot

To: s-news <s-news@wubios.wustl.edu>
Subject: modification to levelplot
From: Carlisle Thacker <thacker@aoml.noaa.gov>
Date: Mon, 17 Dec 2001 08:51:10 -0500 (EST)
In response to my query concerning levelplot:

   I have data in the form of a data frame with column x,y,z, where x,y
   are evaluated a rectangular array.  However, x,y do not range over all
   possible elements of the rectangular array.  When there data for most
   of the elements, levelplot works as expected.  But when there are
   relatively few elements with data, it doesn't; rectangles larger than
   the array elements appear.

   Any suggestions?

The suggestions were to alter my data and fill the array.  My intent
was to modify levelplot rather than the data.  As it turned out, this
was not too difficult.

The problem was caused by levelplot knowing nothing about the xy-grid
other than what it can infer from the data it receives.  This suggests
the modifications are needed for it to accept this information.  Below
is my modification to panel.levelplot, which allows information about
the grid geometry to be specified in levelplot:

my.panel.levelplot <-
   function(x, y, z, subscripts, at, col.regions, 
   contour = F, region = T, x.centers = x, y.centers = y, ...)  {
#
# modification of panel.levelplot that allows specification of the 
# locations of grid cells
# intended to overcome problem of very sparse data
#
# x.centers contains x-coordinates of grid cells
# x SHOULD BE A SUBSET OF x.centers
# similar for y.centers
#
           z <- z[subscripts]
           good <- !(is.na(x) | is.na(y) | is.na(z))
           x <- x[good]
           y <- y[good]
           z <- z[good]
   # make vectors of cell centers
#          ux <- sort(unique(x))
# replace x with x.centers
           ux <- sort(unique(x.centers))     # x centers
#          uy <- sort(unique(y))
# replace y with y.centers
           uy <- sort(unique(y.centers))     # y centers
   # fill array of cells with NA
           Z <- matrix(NA, length(ux), length(uy))
   # overwrite NAs with data
           Z[cbind(match(x, ux), match(y, uy))] <- z
           if(region)
                render.level.trellis(ux, uy, Z, at, col.regions)
           if(contour)
                render.contour.trellis(ux, uy, Z, at, ...)
   }

When using it, the levelplot call should include something like:

   ...
 x.centers = x.center.data,   # vector of x.coordinates of grid cells
 y.centers = y.center.data,   # vector of y.coordinates of grid cells
     panel = my.panel.levelplot,
   ...

If anyone has further improvements, please let me know.

********************************************************
* William Carlisle Thacker                             *
*                                                      *
* Atlantic Oceanographic and Meteorological Laboratory *
* 4301 Rickenbacker Causeway                           *
* Miami, Florida 33149 USA                             *
* Office:  (305) 361-4323                              *
* Fax:     (305) 361-4392                              *
********************************************************



<Prev in Thread] Current Thread [Next in Thread>
  • modification to levelplot, Carlisle Thacker <=