1

I have spent days searching this site and others for a solution, and haven't found it yet. If there is another page with my solution, and I missed it, I apologize.

I found this but reloading ggplot2 and rgdal (after detaching) didn't fix it.

I am using demographic data at the ZCTA (zip code tabulation area) to overlay polygons on a Google terrain map. I am able to get the polygons plotted correctly using qmap, but after I merge in the demographic data, the plots are all wrong. I've tried specifying the order, and playing with the merge. (Heck, I've tried all sorts of things.) I'd love some help with this.

Here

is a working plot, before the merge, and

here is after.

Here's my code:

# shapefile from Census
fips34 <-readOGR(".", "zt34_d00", stringsAsFactors = FALSE)

# zip code areas, 1 row per ZCTA with nonmissing Census data
ptInd <-read.dta("ptIndzcta.dta")

keepzips <- fips34
keepzips@data$id <-rownames(keepzips@data) # create idvar to remerge
keepzipsdat <- fortify(keepzips, region="id") # fortify
keepzipsdat <- keepzipsdat[order(keepzipsdat$order),] # clarify order
keepzipsdat <- join(keepzipsdat, keepzips@data, by="id") # remerge for zcta

qmap("new jersey", zoom = 8, maptype="terrain", color="bw") +
     geom_polygon(aes(x=long, y=lat, group=group), 
           data=keepzipsdat) + coord_equal() # this map plots fine

# now merge in data to create choropleth
zip2 <- merge(keepzipsdat, ptInd, by.y="zcta5", by.x="ZCTA", all.x = TRUE)
zip2[order(zip2$order),] # reestablish order, is this necessary?

qmap("new jersey", zoom = 8, maptype="terrain", color="bw") +
    geom_polygon(aes(x=long, y=lat, group=group), 
           data=zip2) + coord_equal() # this looks crazy
ggplot(data=zip2, aes(x=long, y=lat, group=group)) + geom_polygon() 
# also crazy

# and this is before assigning a fill variable to the polygons
Community
  • 1
  • 1
Sam
  • 11
  • 1
  • 2
    I wonder if my answer in [this post](http://stackoverflow.com/questions/26540955/maps-ggplot2-fill-by-state-is-missing-certain-areas-on-the-map/26541278#26541278) would help you. – jazzurro Sep 28 '15 at 15:17
  • YES! jazzurro, you are a life-saver. Sorry I didn't find your post before! – Sam Sep 28 '15 at 16:13

0 Answers0