[R-sig-Geo] using spplot to superimpose points and polygons on SpatialGridDataFrame
Roger Bivand
Roger.Bivand at nhh.no
Mon Sep 24 07:36:53 CEST 2007
On Sun, 23 Sep 2007, reeves at nceas.ucsb.edu wrote:
>
>
> Hello:
>
> Thanks to the excellent example at:
>
> http://casoilresource.lawr.ucdavis.edu/drupal/node/442
>
> I am close to my goal of creating a map diagram in R
> using raster and vector data layers.
>
> I have set up a similar diagram using as inputs a satellite image
> and point shapefile:
>
> cPts = readShapePoints("PSCentroidPointShape")
> ProjString = proj4string(MapPolysDataFrame)
> proj4string(cPts) = CRS(ProjString)
> psImg = readGDAL("PugetSoundSub1.img")
> #
> # psImg is a SpatialGridDataFrame
> #
> image(PsImg) # just display the raster
> #
> # one way to superimpose vectors over rasters
> #
> ctysPS = SpatialPolygons2PolySet(ctys)
> points(cPts,add=TRUE)
> addPolys(ctysPS)
Unless you need to go to PolySets, you could say plot(ctys, add=TRUE).
> #
> # now, see if spplot works as indicated in this case by overlaying
> # the points over the grid.
> #
> points <- list("sp.points", cPts, pch = 4, col = "black", cex=0.5)
> greys= grey(0:256 / 256)
> #
> spplot(psImg,zcol="band1",col.regions=greys,main="my
> Plot",sp.layout=list(points))
>
> OK, This generates a diagram, but the points are drawn FIRST, not
> second, so they are not visible on top of the image.
>
With:
library(sp)
data(meuse)
data(meuse.grid)
coordinates(meuse) <- c("x", "y")
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
pts <- list("sp.points", meuse, pch = 4, col = "black", cex=0.5)
greys <- grey(0:256 / 256)
spplot(meuse.grid, "dist", col.regions=greys, sp.layout=list(pts))
though you need to match the # cuts to the col.regions:
spplot(meuse.grid, "dist", col.regions=greys, sp.layout=list(pts),
cuts=length(greys)-1)
I cannot reproduce your problem. Could you try to demonstrate it with the
provided data set, so that we can try to isolate the issue?
Roger
> This conflicts the Web site example, and the spplot help file:
>
> The order of items in sp.layout matters; objects are drawn in the order
> they appear. Plot order and prevalence of sp.layout items: for points and
> lines,
> sp.layout items are drawn before the points (to allow for grids and
> polygons);
>
> ** for grids and polygons sp.layout is drawn afterwards (so the item will
> not be overdrawn by the grid and/or polygon). **
>
> Although a matter of taste, transparency may help when combining things.
>
> The only difference that I can see between my attempt and the web site
> example is that the example uses a SpatialPixelsDataFrame as a base layer.
>
> Can anyone offer insight?
>
>
> And a side question: Is there a way to eliminate the grey level scale on
> the right side of the spplot-generated diagram?
>
> Version of R used: R version 2.5.0 (2007-04-23) (and also version 2.5.1)
>
> I can provide the data files if necessary.
>
> Thanks, Rick R
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list