[R-sig-Geo] ggmap::fortify()
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Mon Dec 9 19:18:46 CET 2013
Because that's how it works.
ggplot likes to have everything in as long a data frame as possible,
rather than a wide data frame. So to plot some polygons with
geom_polygon, for example, you have to create a data frame with one
row per point, and each polygon has a separate id variable (see
?geom_polygon for example).
Same is true for ggmap - fortify puts all the coordinates of each
vector of every polygon into a data frame, adds an id variable and a
bunch of other things for the plot order and whether its a hole or not
etc. Then ggmap uses that to reconstruct the individual polygons to
draw them. Its the ggplot way. Stick everything into as long a data
frame as conceivably possible given the data. Once you realise that,
you'll start to 'get' ggplot.
If you want to use ggplot with SpatialPoints, just get the
coordinates(sppts) and turn into a data frame with x and y coords.
But essentially I don't think ggplot plays nicely with sp class objects.
You can't pass spatial*dataframes into ggplot even if you aren;t using
the spatial components, for example using the scot_BNG from ?readOGR:
It has X_COOR and Y_COOR attributes, lets try and plot those (you
could use any attributes, even non-spatial ones here)
> ggplot(scot_BNG,aes(x=X_COOR,y=Y_COOR))+geom_point()
Regions defined for each Polygons
Error in eval(expr, envir, enclos) : object 'X_COOR' not found
- basically it won't treat it like a vanilla data frame. I don't know
what its trying to do. It seems to have some idea that its got
polygons, but then it just gives up.
You have to convert to a plain vanilla data frame first:
> scot_BNGd=data.frame(scot_BNG)
> ggplot(scot_BNGd,aes(x=X_COOR,y=Y_COOR))+geom_point()
- works
such is code.
On Mon, Dec 9, 2013 at 5:53 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
> Does anyone know why function fortify() (to be run before plotting the
> object with qmap) accepts spatial polygons DF and not
> spatial points DF or spatial pixels DF?
> Thanks
>
> Agus
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list