I have a shapefile composed of polygons (sample here), many of which have holes in them. I cleaned the polygon layer in GRASS. After cleaning, QGIS's 'Check Geometry Validity' tool returned no errors. I then loaded the shapefile to R as follows:

polys <- readOGR(dsn=path.expand("U:/FandR"), layer="polygon")
polysCRS<- spTransform(polygons, CRS("+init=epsg:32717"))
polysCRS.points = fortify(polysCRS, region="id")
polysCRS.df = join(polynsCRS.points, polysCRS@data, by="id")

Unfortunately, upon plotting, I can see that the holes in the polygons are filled.

#plot the polygons!
  geom_path(color="white") +

Here is an image of a portion of my polygon plot, zoomed in to see the worst portion of the plot.

How can I correct this mistake in my loaded SpatialPolygonsDataFrame?

Filled holes in polygon

  • 3
    It is not readOGR but a ggplot2::geom_polygon problem. It can only do one hole per poly, and only sleight of hand anyway. Search "geom_holygon" for a workaround, or use spplot or just plot in sp (that use pathGrob and polypath) – mdsumner Jul 6 '16 at 18:50
  • Thanks, @mdsumner! I edited the title and tag to refer to ggplot2. I'm looking into these options now. – Nematode Jul 7 '16 at 8:17
  • You could fake the holes by telling aes that group is "piece" and setting all hole fill to white – mdsumner Jul 7 '16 at 10:07
  • @mdsumner Thanks for your suggestions! geom_holgon worked for me just beautifully! Although 'faking the holes' would have served adequately for my purposes, I could not make the 'group=piece' modification. I had had to set group=group to prevent polygon tearing, as in here stackoverflow.com/questions/28465040/… ; and when I modified to 'piece' those interrupting lines were reintroduced. – Nematode Jul 8 '16 at 10:00
  • Oh, the pieces must not be unique in fortify, like in raster::geom (Good catch). – mdsumner Jul 8 '16 at 10:17

You can now use package sf and the development version of ggplot2 (e.g. devtools::install_github('tidyverse/ggplot2')).

# load libraries

# read the sample data you provided (I converted to geopackage)
polys <- st_read('poly-hole.gpkg') 

# plot
ggplot() +
  geom_sf(data = polys, fill="#006600", color="white")

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.