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.

11
  • try this package. it makes it easy to do what you want cran.r-project.org/web/packages/choroplethr/choroplethr.pdf Commented Jun 11, 2015 at 17:51
  • 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! Commented Jun 11, 2015 at 17:56
  • oh, i think it branched off into a separate package: github.com/arilamstein/choroplethrZip Commented Jun 11, 2015 at 18:06
  • This is not available through R yet. At least I tried installing choroplethrZip and it said it was not available. Any other ideas? Commented Jun 11, 2015 at 18:10
  • 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. Commented Jun 11, 2015 at 18:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

note as of August 2020, 1.5.0 is out, so install_github('arilamstein/[email protected]') now

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.