[R-sig-Geo] how to plot different rows of a SpatialPolygonsDataFrame in trellis panels

Waichler, Scott R Scott@W@ichler @ending from pnnl@gov
Wed May 23 21:50:20 CEST 2018


Roger,

> Without having tried your code, how similar is this to these packages:
> 
> https://cran.r-project.org/package=micromap
> https://cran.r-project.org/package=micromapST

Thanks for responding.  I looked at the micromap vignettes but it seems their focus is being able to provide small maps in one column in figures where the main emphasis is on statistics presented in other columns.  In my problem, all I want to show are maps.  I haven't been able to do this with a higher-level function like spplot or stplot.  I can imagine doing it with a custom panel function, where lpolygons() is used with input that depends on the panel.  

In case it helps, I should clarify that the "different polygons" I referred to are polygons with different spatial coordinates; the attribute values are a small number of discrete values.  My polygons are representations of contaminant plumes in groundwater.  For each year, I have read in a shapefile containing polygons of the contaminant concentration at 2 to 4 levels.  I have combined all of these data into a SpatialPolygonsDataFrame, where each row represents a given year and concentration level, and contains one or more polygons.  The coordinates are different in each row, but years and concentration level are common across rows.  This seems to match the nature of a "long table" format in spacetime, but I haven't been able to work it out in that package, hence I'm stepping back and trying to get the basics working with spplot.

Thanks,
Scott

> > Hello,
> >
> > I have a SpatialPolygonsDataFrame.  I would like to do a trellis plot on one
> of the attributes, so that in the panel for a given attribute value, only those
> polygons with that value are plotted.  So, each panel has different polygons
> plotted in it.  I can't figure out how to do this.  In the toy example below, I
> would like to create a trellis plot with one panel showing the polygons with id
> = 1, and another panel showing the polygons with id = 2.
> >
> > My goal beyond this toy problem is to do the same thing with stplot, where
> panels correspond to times and each time has a different set of polygons
> plotted.  Will that be possible?  In all the examples I can find of using stplot
> for a space-time grid with the spatial objects being polygons, the polygons
> are the same across time.
> >
> > # based on example in help("SpatialPolygonsDataFrame-class")
> > Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
> > Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
> > Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
> > Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
> > Srs1 = Polygons(list(Sr1), "s1")
> > Srs2 = Polygons(list(Sr2), "s2")
> > Srs3 = Polygons(list(Sr3, Sr4), "s3/4") SpP =
> > SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3) grd <- GridTopology(c(1,1),
> > c(1,1), c(10,10)) polys <- as(grd, "SpatialPolygons") centroids <-
> > coordinates(polys) x <- centroids[,1] y <- centroids[,2] z <- 1.4 +
> > 0.1*x + 0.2*y + 0.002*x*x id = factor(sample(c(1,2),
> > size=length(polys), replace=T)) tmp <- SpatialPolygonsDataFrame(polys,
> >      data=data.frame(x=x, y=y, z=z, id=id,
> > row.names=row.names(polys)))
> > plot(tmp)  # plots all the square polygons (n = 10*10)
> > spplot(tmp)  # plots values of x, y, z, id in separate panels, each
> > with 100 polys spplot(tmp, zcol=z)  # error message about duplication
> > of factor level spplot(tmp ~ id, zcol=z, data=tmp)  # won't take
> > formula
> >
> > Thank you,
> > ScottWaichler
> > Pacific Northwest National Laboratory
> > scott.waichler _at_ pnnl.gov
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
> 
> --
> Roger Bivand
> Department of Economics, Norwegian School of Economics, Helleveien 30, N-
> 5045 Bergen, Norway.
> voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
> http://orcid.org/0000-0003-2392-6140
> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



More information about the R-sig-Geo mailing list