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.
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/choroplethrZip@v1.3.0')
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()
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()
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.