Making a zip code choropleth in R using ggplot2 and ggmap
Asked Answered
R

1

19

I am trying to make a choropleth of very simple data, and it's kind of a pain in the neck. I have the following zip codes in the Eastern USA. This is made up data but you get the idea.

Zip    Freq
11101    10
10014    15
11238   400

etc. for about 100 rows. Values of Freq range from 0-1000, and these are the ones I would like to use to determine the color of each zipcode. I would ideally also like the map to focus on the Eastern USA instead of the whole country.

I want to make a choropleth with this data and each zip code but I can't figure out how to import zip code shapefiles. I have tried this tutorial but I got an error at the fortify() step that I can't get beyond. I'm not sure if that tutorial's method is even the best way to go about it.

ggplot2 seems to come with State and County, but I can't figure out how to map by zip code. (Eventually I am going to map by census tract but right now I just want to learn how to use shapefiles for zip codes and this simple data set)

All the resources I have found for choroplethr use functions that are now deprecated. I spent hours chasing my tail in an effort to use it, and am so frustrated, so any help would be greatly appreciated.

Rhyner answered 11/6, 2015 at 17:47 Comment(11)
try this package. it makes it easy to do what you want cran.r-project.org/web/packages/choroplethr/choroplethr.pdfHoopes
Thank you for responding. Choroplethr's function for doing this, zip_map, is deprecated in the current version. When I try to use it I get the message "This function is deprecated as of choroplethr version 3.0.0. Please use ?zip_choropleth instead." But when I try to use zip_choropleth, it says that function does not exist!Rhyner
oh, i think it branched off into a separate package: github.com/arilamstein/choroplethrZipHoopes
This is not available through R yet. At least I tried installing choroplethrZip and it said it was not available. Any other ideas?Rhyner
If you don't need to fill the zips, you could use this technique. Here is a nice tutorial which appears to be updated. Some (myself included) would argue that "filled" maps are misleading as some large (area) zip codes are not densely populated - this creates a bias in interpretation - thus packages such as rcstatebin have been created.Sauternes
did you do this # install.packages("devtools") library(devtools) install_github('arilamstein/[email protected]')? it's not on cran.Hoopes
Isn't it unsafe to install something that isn't on cran? And @JasonAizkalns, thanks for replying. I am trying to use that method right now but running into trouble with gpclibPermitStatus() and then when I try to insteall gpclib it says "Package which is only available in source form.....These will not be installed." It sucks that this is such an opaque, abysmally documented process. But I appreciate your guidanceRhyner
This shld help with the gpclibPermitStatus issue. The choroplethZip package is safe to use and is probably the easiest way for you to do what you need to do unless you want an end-to-end example on doing it manually in ggplot (from shapefile to choropleth).Shandra
@Shandra thank you for trying, but as I addressed above, install.packages("gpclib") returns an error message that is "is only available in source form." I was hoping to use this as a learning process, so an end-to-end example would be very helpfulRhyner
What's wrong with installing from source? install.packages("gpclib", type="source"). Just grab RTools if you're on Windows.Shandra
See, why didn't I think of that? That is exactly what I needed to do, but I didn't know you could do that with install.packages. Onwards! (Also, feel free to post that answer in the separate question I asked about this issue). Thank you @hrbrmstr!!Rhyner
H
15

Thank you for using choroplethr, and I'm sorry that the deprecation of zip_map caused you problems. I have moved all ZIP related functions to a separate packaged called choroplethrZip.

The old verion of choroplethr rendered ZIPs as scatterplots, not choropleths. Rendering them as proper choropleths required a map that is too large for CRAN (~60MB), which is why it is only available via github.

The github page I link to above has 3 vignettes. Basically, the function zip_choropleth should do exactly what you want, and work like all the other choroplethr functions. You want to use the state_zoom to zoom in on the east coast states:

# use the devtools package from CRAN to install choroplethrZip from github
install.packages("devtools")
library(devtools)
install_github('arilamstein/[email protected]')
library(choroplethrZip)

data(df_pop_zip)

# ec = east coast
ec_states = c("maine", "new hampshire", "massachusetts", "rhode island", "connecticut", 
              "new york", "new jersey", "delaware", "maryland", 
              "virginia", "north carolina", "south carolina", "georgia", "florida",
              "pennsylvania", "district of columbia", "vermont", "west virginia")

zip_choropleth(df_pop_zip, 
               state_zoom = ec_states, 
               title      = "2012 ZCTA Population Estimates",
               legend     = "Population") + coord_map()    

enter image description here

The resulting map is essentially unreadable because the zips are so small that all you can see are the borders. If you want to remove the borders, try this:

choro = choroplethrZip::ZipChoropleth$new(df_pop_zip)
choro$prepare_map()

data(zip.regions)
choro$legend = "Population"
ec_zips = zip.regions[zip.regions$state.name %in% ec_states, "region"]
ec_df   = choro$choropleth.df[choro$choropleth.df$region %in% ec_zips, ]
ec_plot = choro$render_helper(ec_df, "", choro$theme_clean()) + 
              ggtitle("2012 ZCTA Population Estimates")

ec_plot + coord_map() 

enter image description here

In the future, I might add an option that makes it easier to render the maps with no borders. But for now (version 1.3.0) this is the easiest way I can see to do it, and is basically what I do behind the scenes to render the national zip maps, which themselves are rendered without borders.

Note that coord_map just forces a mercator projection.

Hopkins answered 15/6, 2015 at 23:19 Comment(1)
note as of August 2020, 1.5.0 is out, so install_github('arilamstein/[email protected]') nowJotham

© 2022 - 2024 — McMap. All rights reserved.