Combining Choropleth Maps and Reference Maps in R

September 30, 2015
By

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

Recent updates to my mapping packages now make it easy to combine choropleth maps and reference maps in R. All you have to do is pass the parameter reference_map = TRUE to the existing functions. This should “just work”, regardless of which region you zoom in on or what data you display. The following table shows the affected functions and their packages.

Map Function Package
US States state_choropleth choroplethr
US Counties county_choropleth choroplethr
US ZIP Codes zip_choropleth choroplethrZip
California Census Tracts ca_tract_choropleth choroplethrCaCensusTract

If you want to learn more about how to use these packages, sign up for my free email course Learn to Map Census Data in R.

Install the Packages

Here is how to get the version of the packages that have these changes:

install.packages("choroplethr")

# install.packages("devtools")
library(devtools)
install_github('arilamstein/[email protected]')
install_github("arilamstein/[email protected]")

In my experience reference maps provide the most value when viewing small regions. So let’s start with viewing the smallest geographic unit that my packages support: Census Tracts.

Census Tracts

Consider this choropleth map which shows income estimates of census tracts in Los Angeles county from 2013:

Some natural questions this map raises are “What is that large tract in the northeast?” and “Why is Los Angeles county discontiguous?” Both of these questions can be easily answered by combining the choropleth map with a reference map:

The large tract in the northeast is a forest, and Los Angeles is discontiguous because it has two large islands. Here is the code to create those maps:

library(choroplethrCaCensusTract)
data(df_ca_tract_demographics)
df_ca_tract_demographics$value = df_ca_tract_demographics$per_capita

ca_tract_choropleth(df_ca_tract_demographics,
    title       = "2013 Los Angeles Census Tractn Per Capita Income",
    legend      = "Income",
    num_colors  = 4,
    county_zoom = 6037)

ca_tract_choropleth(df_ca_tract_demographics,
    title         = "2013 Los Angeles Census Tractn Per Capita Income",
    legend        = "Income",
    num_colors    = 4,
    county_zoom   = 6037,
    reference_map = TRUE)

ZIP Codes

Consider this choropleth which shows income estimates of Manhattan Zip Code Tabulated Areas (ZCTAs) in 2013:

manhattan-zip-1

Someone not familiar with Manhattan’s geography might wonder what the dark neighborhood on the east is, and what the light neighborhood in the north is. Combining the choropleth map with a reference map answers those questions.

manhattan-zip-2

The low-income neighborhood in the north is Harlem, and the high income neighborhood in the east is the Upper East Side.

Here is the source code to create those maps:

library(choroplethrZip)
data(df_zip_demographics)
df_zip_demographics$value = df_zip_demographics$per_capita_income

zip_choropleth(df_zip_demographics,
    title       = "2013 Manhattan ZIP Code Income Estimates",
    legend      = "Per Capita Income",
    county_zoom = 36061)

zip_choropleth(df_zip_demographics,
    title         = "2013 Manhattan ZIP Code Income Estimates",
    legend        = "Per Capita Income",
    county_zoom   = 36061,
    reference_map = TRUE)

Counties

Reference maps can also be useful when viewing county choropleths. Consider this map which shows county populations in California:

ca-county-1

A common question when viewing this map is “What is the large, low-population county on the eastern part of the state?” Adding a reference map allows us to easily answer the question:

ca-county-2

The county in question contains Death Valley – the hottest, driest and lowest point in North America.

Here is the code to produce those maps:

library(choroplethr)
data(df_pop_county)
county_choropleth(df_pop_county,
    title      = "2012 California County Population Estimates",
    legend     = "Population",
    state_zoom = "california")
 
county_choropleth(df_pop_county,
    title         = "2012 California County Population Estimates",
    legend        = "Population",
    state_zoom    = "california",
    reference_map = TRUE)

States

You can also combine choropleth maps with reference maps at the state level:

state-1state-2

At this time you cannot make reference maps with maps that contain insets, such as maps of the 50 US states. Here is the code to produce those maps:

library(choroplethr)
data(df_pop_state)
data(continental_us_states)

state_choropleth(df_pop_state,
    title  = "2012 State Population Estimates",
    legend = "Population",
    zoom   = continental_us_states)

state_choropleth(df_pop_state,
    title         = "2012 State Population Estimates",
    legend        = "Population",
    zoom          = continental_us_states,
    reference_map = TRUE)

More Information

If you are curious about how this code works, then look at the function render_with_reference_map. If you have technical questions, the best place to ask is the choroplethr google group.

LEARN TO MAP CENSUS DATA
Subscribe and get my free email course: Mapping Census Data in R!

100% Privacy. We don’t spam.


The post Combining Choropleth Maps and Reference Maps in R appeared first on AriLamstein.com.

To leave a comment for the author, please follow the link and comment on their blog: AriLamstein.com » R.

R-bloggers.com 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.

Sponsors

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)