2

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?

2
  • When I try to run your code, I get an error in readOGR(.,'cb_2013_us_county_500k' ) that Error in nchar(dsn) : object '.' not found. Can you please make your code reproducible? Commented Nov 22, 2014 at 16:54
  • Also, in which package is the function join() ? Commented Nov 22, 2014 at 17:00

1 Answer 1

3

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

1 Comment

In similar problem your solution works for me, but only if I use group=group as you write in the block of code. The group=id version still generates artifacts.

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.