User Question: How to Add a State Border to a Zip Code Map?

September 10, 2015

(This article was first published on » R, and kindly contributed to R-bloggers)

A user recently asked me how to add a state outline to a Zip Code map. Consider this map, which shows the percent white of each Zip Code Tabulated Area (ZCTA) in New York and New Jersey:


Clearly the ZCTAs in northern New York have the highest concentration of white residents. However, it would still be useful to see the border between New York and New Jersey.

Step 1: Build the Initial Map

This map has one complexity to it: it hides the actual ZCTA borders. This technique is very useful when viewing a large number of Zip codes and is described in detail here.

# Load the necessary packages
install_github('arilamstein/[email protected]')

# load the data 
df_zip_demographics$value = df_zip_demographics$percent_white

# create the map
zip_map = ZipChoropleth$new(df_zip_demographics)
zip_map$ggplot_polygon = geom_polygon(aes(fill = value), 
                                      color = NA)
zip_map$set_zoom_zip(state_zoom  = c("new york", "new jersey"), 
                     county_zoom = NULL, 
                     msa_zoom    = NULL, 
                     zip_zoom    = NULL) 
zip_map$title = "New York and New Jersey ZCTAs"
zip_map$legend = "Percent White"
choro = zip_map$render()

Step 2: Create the State Outlines

State outlines are rendered in the render_state_outline function in the USAChoropleth object. Here is how to store the outline:

outline = StateChoropleth$new(df_pop_state)
outline = outline$render_state_outline(c("new york", "new jersey")) 

Step 3: Render the Combined Maps

The zip map and the outline are both ggplot objects. This means that they can be combined with the + sign:

choro_with_outline = choro + outline


The post User Question: How to Add a State Border to a Zip Code Map? appeared first on

To leave a comment for the author, please follow the link and comment on their blog: » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)