[R-sig-Geo] How to draw a legend when plotting sf (simple feature) objects

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sun Feb 26 18:46:25 CET 2017



On 26/02/17 13:23, Manuel Spínola wrote:
> Dear list members,
> 
> How can I draw a legend when plotting an sf (simple feature) object.
> 
> nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)
> 
> plot(nc["SID79"])
> 

You're in base plot, so at this moment you can add elements
incrementally to the plot, see e.g. ?legend.

I have no plans of making this easy or automatic, because I don't think
that base plot is the right place for it (although packages raster,
spatstat and fields try hard), but would happily consider contributions.

geom_sf in ggplot2 (now in sf branch on github) will follow up
sp::spplot for sf objects; see e.g.

https://github.com/edzer/sfr/issues/88#issuecomment-276738460

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) + geom_sf(aes(fill = SID79))

Trying this with the gpkg you read in above fails for me; it looks like
ggplot now wrongly assumes that geometry columns are always called
`geometry' (despite the docs), which is not the case.
-- 
Edzer Pebesma
Institute for Geoinformatics  (ifgi),  University of Münster
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20170226/ab55b362/attachment.bin>


More information about the R-sig-Geo mailing list