Combining Choropleth Maps and Reference Maps in R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
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.
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:
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:
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:
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.
The post Combining Choropleth Maps and Reference Maps in R appeared first on AriLamstein.com.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.