[R-sig-Geo] ggmap::fortify()
Agustin Lobo
alobolistas at gmail.com
Tue Dec 10 09:38:28 CET 2013
Thanks.
The example with an spatial polygons DF in p. 159 of
http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf
works nicely.
Instead, fortify() refuses processing spatial poins or pixels DF
with an error:
Error: ggplot2 doesn't know how to deal with data of class
SpatialPixelsDataFrame
Based on what you say, using
adf <- dataframe(a)
where a is an spatial points DF in epsg 4326 (== CRS("+proj=longlat
+ellps=WGS84 +no_defs"))
results into a dataframe adf that is as the table of a but including
the coordinates as well.
Then doing the equivalent to that explained in the pdf for the
polygons seems to work.
It might be that for the case of spatial points DF, dataframe() is
enough and fortify() is just not required.
Agus
On Mon, Dec 9, 2013 at 7:18 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> 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
>
> _______________________________________________
> 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