1

I'm trying to learn how to plot shapefiles with ggplot2 and I found a wiki at:

https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles

I downloaded and extracted the zip file provided into new directory on my desktop ( http://www.epa.gov/nheerl/arm/documents/design_doc/ecoregion_design.zip ).

I started reading through the article, going line by line but I'm running into a problem with fortify.

require("rgdal")
require("maptools")
require("ggplot2")
require("plyr")

> my_dsn <- "directory here"
> 
>   utah = readOGR(dsn=my_dsn, layer="eco_l3_ut")
OGR data source with driver: ESRI Shapefile 
Source: "directory here", layer: "eco_l3_ut"
with 10 features and 7 fields
Feature type: wkbPolygon with 2 dimensions
>   utah@data$id = rownames(utah@data)
>   utah.points = fortify(utah, region="id")
Error: isTRUE(gpclibPermitStatus()) is not TRUE

I looked at the ?fortify help page and get the jist of how it should be used. I think its being used correctly. I also found another question with the same error on SO,

Getting error while using fortify function in R (ggplot)

but I can't figure out how to apply what is being said for my solution. I might help if I had access to dropbox right now but I don't.

Community
  • 1
  • 1
cylondude
  • 1,816
  • 1
  • 22
  • 55
  • possible duplicate of [Converting spatial polygon to regular data frame without use of gpclib tools](http://stackoverflow.com/questions/21125942/converting-spatial-polygon-to-regular-data-frame-without-use-of-gpclib-tools) – Brian Diggs Feb 16 '14 at 00:48

1 Answers1

2

The short answer is to install the rgeos package and include it at the top.

A longer answer can be found here (and elsewhere): https://stat.ethz.ch/pipermail/r-sig-geo/2012-July/015685.html

maxliving
  • 110
  • 1
  • 2
  • Installing and loading the rgeos package is not solving the problem immediately. I'll try and read through the link. it seems like it is not defaulting to rgeos but is instead using gpclib. the same error remains. – cylondude Feb 13 '14 at 18:39
  • Did you reload maptools after installing and loading rgeos? I remember having this problem myself and eventually getting it to work, but am having trouble retracing my steps. But you definitely can do it without gpclib. – maxliving Feb 13 '14 at 18:51
  • I just tried reloading maptools after rgeos but no luck. does the example work for you? – cylondude Feb 13 '14 at 18:55
  • Here's a project where I use it. If I have some time later I can reduce it to a MWE, but most of what you need is toward the top: https://github.com/maxliving/stopnfrisk/blob/master/programs/stop_frisk_maps.R. – maxliving Feb 14 '14 at 15:34
  • One issue might be OS. What are you running? I've gotten this working on Mac and Unix, but haven't tried on Windows. – maxliving Feb 14 '14 at 15:35
  • Does reloading `ggplot2` help? Fortify is part of the ggplot library and it will load `rgeos` when you call it. – cengel Feb 14 '14 at 18:41
  • I tried reinstalling ggplot2 and it worked. Thanks. – cylondude Feb 18 '14 at 22:23