Many of us have become addicted to The NY Times COVID maps — maps of US state or county level data colored by cases, vaccinations, per capita infections, etc. While recreating maps like these in R is possible, it is disappointingly difficult. The just released MazamaSpatialPlots R package takes a first stab at remedying this situation.
NY Times Maps
One of the key features of the maps displayed on the NY Times web site is the use of an appropriate projection for each nation or state being shown. Another feature is the use of thin, light colored lines around states and counties. The result is both informative and attractive.
Creating Beautiful Maps in R
We would like to produce similarly attractive state- and county-level maps in R with as little effort as possible. There is a lot of state- and county-level tabular data out there that can be easily harvested and the MazamaSpatialPlots package makes it easy to convert tables of data into attractive maps.
Here is a start-to-finish example that: 1) ingests county level data; 2) adds required columns of state and county identifiers; and 3) creates an attractive map:
library("MazamaSpatialPlots") # Ingest county level data # See: https://healthinequality.org/dl/health_ineq_online_table_12_readme.pdf URL <- "https://healthinequality.org/dl/health_ineq_online_table_12.csv" characteristicsData <- read.csv(URL) # Added required 'stateCode' and 'countyFIPS' variables characteristicsData <- characteristicsData %>% dplyr::mutate( stateCode = stateabbrv, countyFIPS = MazamaSpatialUtils::US_countyNameToFIPS(stateCode, county_name), pUninsured2010 = puninsured2010, .keep = "none" ) # Create map countyMap( data = characteristicsData, parameter = 'pUninsured2010', legendTitle = 'Uninsured (%)', title = "Percentage of population uninsured in 2010" )
A few additional features of the package are demonstrated in the next two plots:
- state or county level maps
- subset by state or group of states
- automatic calculation of the most appropriate projection
- important attributes are configurable in the top level function calls
- graphical object is a tmap and a ggplot object that can be further customized with those packages
The MazamaSpatialPlots package is built on top of MazamaSpatialUtils and utilizes harmonized spatial datasets from that package that must be installed. These include US Census state and county datasets that have been simplified to 1, 2 and 5% of the original size so that you can create a national map quickly at a lower level of detail yet still show lots of detail for smaller areas if you want.
Preparing Input Data
Any data frame can be passed to the
countyMap() functions as long as it meets the following criteria:
- No more than one record should be present for each state or county.
- State level data must include a
stateCodecolumn with the 2-character postal abbreviation (aka ISO 3166-2 alpha-2).
- County level data must include a
countyFIPScolumn with the 5-digit county FIPS code.
You can use MazamaSpatialUtils conversion functions to help with the creation of these columns.
You can use functions from readr to quickly ingest tabular data or MazamaCoreUtils::html_getTable() to scrape tabular data from a web page.
There is a ton of useful data out there and our goal is to make it a very simple task to convert that data into attractive, informative maps.
The function signature for
countyPlot() shows top level configurable parameters:
data = NULL,
parameter = NULL,
state_SPDF = "USCensusStates_02",
county_SPDF = "USCensusCounties_02",
palette = "YlOrBr",
breaks = NULL,
style = ifelse(is.null(breaks), "pretty", "fixed"),
showLegend = TRUE,
legendOrientation = "vertical",
legendTitle = NULL,
conusOnly = TRUE,
stateCode = NULL,
projection = NULL,
stateBorderColor = "gray50",
countyBorderColor = "white",
title = NULL
The plotting inside MazamaSpatialPlots utilizes the excellent tmap package for “thematic mapping” which is in turn built on top of ggplo2. So there is a tremendous amount of customization that can be done through those packages. Numerous examples are provided at the MazamaSpatialPlots website.
Best of luck creating beautiful maps!