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.

4
  • 1
    Maybe take a look here : github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles Commented Sep 26, 2013 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 Commented Sep 26, 2013 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. Commented Sep 26, 2013 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) 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! Commented Sep 26, 2013 at 13:15

2 Answers 2

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))
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.