[R-sig-Geo] Differing pch symbols for categorical data in spplot
Sarah Goslee
sarah.goslee at gmail.com
Mon Dec 20 22:12:27 CET 2010
Hi Lee,
Here's a neat way to do that (with fake data, since it doesn't actually have
anything to do with polygons):
pointclass <- c(1, 2, 1, 1, 3, 2)
classcol <- c("red", "blue", "green")
classpch <- c(5, 17, 3)
plot(1:6, 1:6, col=classcol[pointclass], pch=classpch[pointclass])
Sarah
On Mon, Dec 20, 2010 at 4:00 PM, Lee Hachadoorian
<Lee.Hachadoorian+L at gmail.com> wrote:
> I managed to get something working here, but can't take it any further. I
> would like to map points on top of polygons using spplot, and control the
> pch symbols and colors used by the points, which represent categorical
> data.
>
> I was able to get this partially working. An example using data included
> with the sp package is:
>
> #LOAD LIBRARY
> library(sp)
>
> #CREATE SpatialPolygonsDataFrame OF RIVERBED
> data(meuse.riv)
> meuse.lst = list(Polygons(list(Polygon(meuse.riv)), "meusee.riv"))
> spdfMeuse = SpatialPolygonsDataFrame(
> SpatialPolygons(meuse.lst),
> data.frame(anything = 1, row.names = "meusee.riv")
> )
>
> #CREATE SpatialPoints OF SOIL SAMPLES
> data(meuse)
> coordinates(meuse) = c("x", "y")
>
> #PLOT
> spplot(spdfMeuse,
> sp.layout = list(
> "sp.points",
> meuse,
> pch = as.integer(meuse$soil)
> )
> )
>
> Since I'm casting soil (a 3-level factor) to integer, I can pass it as an
> argument to pch. But this gives me very limited control over the symbols
> used. I can add an integer to get, for example, pch 15, 16, & 17, but what
> if I want to use pch 5, 13, & 17? Also, I can set the color with col =
> "whatever", but I haven't been able to figure out how to print three
> different colors that are linked to the three factors.
>
> Any suggestions on how to accomplish this or otherwise improve this call
> to spplot would be greatly appreciated.
>
> Regards,
> Lee Hachadoorian
> PhD Student, Geography
> Program in Earth & Environmental Sciences
> CUNY Graduate Center
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-sig-Geo
mailing list