2

I have a specific question: How can I choose either fill or color of a ggplot according to the data of an SpatialPolygonsDataFrame-object? For example consider the following SpatialPolygonsDataFrame sf:

sf <- readShapePoly("somePolygonShapeFile")

It allows me to access the the example data field FK like:

sf$FK            // or
sf@data$FK

Now, I want to prepare a simple ggplot:

p <- ggplot(sf, aes(x=long, y=lat, group=group, FK=???))

However, I don't know what to pass to FK in aes(). Experiences from gridded data frames (grid.extent(...)) made me think, I could directly put in FK=FK. This does not seem to work for SpatialPolygonsDataFrame-objects. Trying FK=sf$FK or FK=sf@data$FK is not allowed because:

Error: Aesthetics must either be length one, or the same length as the data

I guess, the solution is trivial, but I simply don't get it at the moment.

Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32
  • 1
    Maybe take a look here : https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles – juba Sep 26 '13 at 11:42
  • @juba I've tried to be as close to the tutorial code as possible. However, utah.points = fortify(utah, region="id") only returns an error: `isTRUE(gpclibPermitStatus()) is not TRUE` – Florian R. Klein Sep 26 '13 at 12:18
  • 2
    just type `gpclibPermit()` or install the `rgeos` package. The `gpclibPermit` function is used to choose to permit the use of `gpclib` if installed, and `gpclibPermitStatus` reports its status. The licence for `gpclib` is not Free or Open Source and explicitly forbids commercial use. – rcs Sep 26 '13 at 12:57
  • @rcs Finally the crux. I hadn't installed gpclib and wasn't able to give me gpclibPermit as a result. Now I can fortify using a region. Hopefully the manual descriptions ([link](https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles)) work for me as well now, so that I can easily transfer the shapefile data field content into the dataframe. Thank you for mentioning the requirements explicitly! – Florian R. Klein Sep 26 '13 at 13:15

2 Answers2

4

Thanks to @juba, @rsc and @SlowLearner I've found out, that the installation of gpclib was still missing to be able to give the gpclibPermit. With this done, fortifying sf using a specified region is not problem anymore. Using the explanation from ggplot2/wiki I am able to transfer all data fields of the original shapefile into a plotting-friendly dataframe. The latter finally works as was intendet for plotting the shapefile in R. Here is the final code with the actual workingDir-variable content left out:

require("rgdal") # requires sp, will use proj.4 if installed
require("maptools")
require("ggplot2")
require("plyr")

workingDir <- ""

sf <- readOGR(dsn=workingDir, layer="BK50_Ausschnitt005")
sf@data$id <- rownames(sf@data)
sf.points <- fortify(sf, region="id")
sf.df <- join(sf.points, sf@data, by="id")

ggplot(sf.df,aes(x=long, y=lat, fill=NFK)) + coord_equal() + geom_polygon(colour="black", size=0.1, aes(group=group))
Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32
1

First, you should use the readOGR function from the rgdal library to read your shapefile (then you won't have problems with gpclib). Here is an example of how to do that.

Second, are you trying to pass the sf object to ggplot as-is? If so, you need to use fortify() to convert your spatial object into a data frame. There should be some kind of identifying column in sf@data such as ID or NAME. So try something like:

sf.df <- fortify(df, region = "NAME")

...and use sf.df for plotting using ggplot.

Community
  • 1
  • 1
SlowLearner
  • 7,907
  • 11
  • 49
  • 80
  • Your answer and the cited example helped understanding the context better. I still needed to join the indivual points of the polygon by the previously created polygon ids as referenced in the comment of @juba in the first post. Still, you have my thanks. – Florian R. Klein Sep 26 '13 at 13:25