[GIS] ggplot2 fills holes in polygons

donut-polygonsggplot2r

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@data$id=rownames(polysCRS@data)
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!
ggplot(polysCRS.df)+
  aes(long,lat,group=group)+
  geom_polygon(fill="#006600")+
  geom_path(color="white") +
  coord_equal()

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

Best Answer

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

# load libraries
library(sf)
library(ggplot2)

# 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