Differing pch symbols for categorical data in spplot

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Differing pch symbols for categorical data in spplot

Lee Hachadoorian
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

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Differing pch symbols for categorical data in spplot

Dylan Beaudette-2
On Mon, Dec 20, 2010 at 1:00 PM, Lee Hachadoorian
<[hidden email]> 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.
>

Hi Lee,

One of the tricks required to get lattice graphics to do what you want
is to master either the par.settings argument to lattice plotting
functions (e.g. spplot) or the trellis.par.set function.

Be sure to call trellis.par.set() each time you start a new device.

Here is a link to a document on this very topic (note shameless self-promotion):
http://casoilresource.lawr.ucdavis.edu/drupal/node/962

Cheers,
Dylan

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Differing pch symbols for categorical data in spplot

Sarah Goslee
In reply to this post by Lee Hachadoorian
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
<[hidden email]> 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

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Differing pch symbols for categorical data in spplot

Lee Hachadoorian
In reply to this post by Dylan Beaudette-2
Sarah, thank you, that worked great.

Dylan, thanks as well. I see you're using the same trick as Sarah in the
relevant argument. The rest of your blog will keep me busy for quite some
time.

Best,
Lee Hachadoorian
PhD Student, Geography
Program in Earth & Environmental Sciences
CUNY Graduate Center

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo