[R-sig-Geo] How can I plot a shapefile with geom_map from ggplot2
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Sun Jul 22 22:38:34 CEST 2012
2012/7/22 Manuel Spínola <mspinola10 at gmail.com>:
> Dear list members,
>
> Sorry for cross-posting.
>
> I asked this question to a ggplot2 forum, but I would like to have other
> suggestions from this list.
>
> How can I plot a shapefile with the new function geom_map from ggplot2?
Well, R is an object-oriented system, so in an ideal world you'd just
pass it anything that geom_map is capable of doing whatever it is that
geom_map needs to do on an object as its 'map' parameter.
Sadly, geom_map, like most ggplot2 functions, is quite fussy about
its food. It likes its map objects fortified, much like corn flakes or
low-fat milk. So you have to fortify it. Literally:
myspdf=readOGR(".","myshapes")
fort = fortify(myspdf)
And you might also have to specify an id. The help(geom_map) isn't
too clear. Then you have to figure out how to get ggplot() and
geom_map() and anything else to talk to each other. At this point you
may consider just doing:
plot(myspdf, col=somefunction(myspdf$Value))
where somefunction() maps your values to colours, like a colourPalette
function. Or even:
spplot(myspdf, "Value")
I tried to do some maps with geom_map for my spatial data workshop
but I hit this documentation problem and a couple of bugs - if I'd had
more time I would have worked it out, but as it stood it darn near
broke me.
Okay, here's how to plot the regions of the columbus dataset from the
spdep package (see example(columbus)) shaded by the AREA within the
given limnits:
ggplot(columbus at data)+geom_map(
map=fortify(columbus,region="id"),
aes(map_id=id,fill=AREA)) +
expand_limits(x=c(5,12),y=c(10,15))
Barry
More information about the R-sig-Geo
mailing list