Chandler McClellan Chandler McClellan - 1 month ago 15
R Question

Artifacts in plotting shapefiles with ggplot2

In attempting to plot U.S. Counties from the Census Bureau's 2013 shapefile, the resulting plot has holes/weird fills, especially around counties with discontinuous regions. I think it has something to do with the order of the the points that are plotted, but am not sure how it can be fixed. For example, a plot of Florida, particularly around the Keys produces:

library(ggplot2)
library(dplyr)
library(rgdal)

download.file('http://www2.census.gov/geo/tiger/GENZ2013/cb_2013_us_county_500k.zip',
'county.zip')
unzip('county.zip')

uscounties <- readOGR(.,'cb_2013_us_county_500k' )
uscounties@data$id <- rownames(uscounties@data)
countypoints <- fortify(uscounties, region='id')
countydf <- join(countypoints, uscounties@data, by='id')

ggplot(countydf[countydf$STATEFP=='12' ,])+
aes(long, lat, group=COUNTYNS, fill=STATEFP)+
geom_polygon()+
geom_path(color="black")+
coord_equal()


Here is the resulting plot :

Florida is just an example. Most other states will display similar errors in plotting as well. Any ideas on how to fix?

Answer

Why are you setting group=COUNTYNS?? Use group=id.

ggplot(countydf[countydf$STATEFP=='12' ,])+
  aes(long, lat, group=group, fill=STATEFP)+
  geom_polygon()+geom_path(color="black")+coord_equal()

You need to take more care when posting a question, so as not to waste peoples' time.. join(...) is in package plyr not dplyr, and

readOGR(.,"...")

doesn't work, you need

readOGR(dsn=".",layer="...")
Comments