[R-sig-Geo] suggestion for sp.grid()
Dylan Beaudette
dylan.beaudette at gmail.com
Wed Dec 15 08:32:47 CET 2010
Hi,
I was tinkering around with some of the new functions from the
latticeExtra package, and thought of a (messy) hack for plotting
multiple grids. However, due to the way sp.grid() is currently coded,
there is no way to define both grid fill color _and_ outline color:
gp = gpar(fill = col, col = NULL, alpha = alpha)
if this line is changed to...
gp = gpar(fill = col, col = NA, alpha = alpha)
then outlines will default to no color... It might be nice to add a
new parameter to sp.grid() to allow the user to set the outline color
of the rectangles produced by grid.rect(). If the above line is
changed so that col=NA, then it is possible to overlay two grids (kind
of) like this:
library(gstat)
library(latticeExtra)
library(grid)
# load example data
data(meuse.grid)
coordinates(meuse.grid) <- ~ x + y
gridded(meuse.grid) <- TRUE
# hacked version of sp.grid()
sp.grid <- function (obj, col = 1, alpha = 1, ...)
{
if (is.character(obj))
obj = get(obj)
xy = coordinates(obj)
if (length(col) != 1 && length(col) != nrow(xy)) {
}
gt = as(getGridTopology(obj), "data.frame")
grid.rect(x = xy[, 1], y = xy[, 2], width = gt$cellsize[1],
height = gt$cellsize[2], default.units = "native", gp =
gpar(fill = col, col = NA, alpha = alpha))
}
# nice colors
trellis.par.set(regions=custom.theme()$regions,
superpose.polygon=list(col='black', alpha=0.25))
# first grid covers entire extent
p3 <- spplot(meuse.grid, 'dist', colorkey=list(space='bottom',
width=1, height=0.5, tick.number=3)
# overlay partially transparent, kind of a hack...
p3 <- p3 + layer(sp.grid(meuse.grid[meuse.grid$soil == 'A', ],
col='black', alpha=0.25))
# add a new key
p3 <- update(p3, key=simpleKey('Bad Stuff', points=FALSE, lines=FALSE,
rect=TRUE, columns=1, space='top'))
print(p3)
Note that I am using an older version of gstat, version 0.9-63. Maybe
this code has been updated since then. If not, what do you think? Are
there any better ways to overlay a couple grids?
Cheers,
Dylan
More information about the R-sig-Geo
mailing list