knitr::opts_chunk$set(echo = TRUE)
The goal of this document is to serve as a tutorial for creating customized maps of U.S. states and territories by using custom projections or the tmap package.
In general, the default projections in stateMap()
are appropriate for common
state groupings. However, the projection parameter can be used to override the
default projections as needed.
Below is an unusual group of states with the default projection.
library(MazamaSpatialPlots) northernStates <- c("AK", "WA", "ID", "MT", "ND", "MN", "WI", "MI", "OH", "PA", "NY", "VT", "NH", "ME") stateMap( data = example_US_stateObesity, parameter = 'obesityRate', palette = 'brewer.yl_or_rd', breaks = seq(23, 33, 2), stateCode = northernStates, stateBorderColor = 'black', title = "Obesity Rate in Northern U.S. States" )
The projection is oddly rotated in this case. To remedy this, a manual projection can be defined:
myproj <- "+proj=lcc +lat_1=32.5 +lat_2=71.4 +lat_0=51.9 +lon_0=-102.9 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs" stateMap( data = example_US_stateObesity, parameter = 'obesityRate', palette = 'brewer.yl_or_rd', breaks = seq(23, 33, 2), stateCode = northernStates, projection = myproj, stateBorderColor = 'black', title = "Obesity Rate in Northern U.S. States" )
One benefit of using stateMap()
is that the output is a ggplot2 object.
Therefore, maps can be further customized using functionality from the tmap
and ggplot2 packages. Many visual adjustments including legend and title
locations, fonts, and background colors can be made by simply appending your
stateMap()
plot with arguments in tmap::tm_layout()
. Furthermore,
tmap::tm_style()
can be used to leverage built in styles and
tmap::tm_compass()
can be used to add a compass to your map. The three
examples below demonstrate how to build customized visualizations using both
stateMap()
parameters and tmap functionality.
stateMap( data = example_US_stateObesity, parameter = "obesityRate", breaks = seq(20,38,3), #increasing color detail conusOnly = FALSE , stateBorderColor = 'black', legendPosition = tmap::tm_pos_in('right', 'top') ) + tmap::tm_layout( frame = TRUE, bg.color = "grey85", inner.margins = .05, ) + tmap::tm_title_out( text = 'Obesity Rates in U.S. States and Territories', fontface = 2, fontfamily = "serif" )
stateMap( data = example_US_stateObesity, parameter = "obesityRate", breaks = seq(20,40,4), ) + tmap::tm_style( 'classic' ) + tmap::tm_layout( inner.margins = .08 ) + tmap::tm_title_out( text = 'Obesity Rate by State', size = 1.2 ) tmap::tm_compass()
stateMap( data = example_US_stateObesity, parameter = "obesityRate", stateCode = c('ME', 'NH', 'VT', 'MA', 'RI', 'CT'), stateBorderColor = 'black', ) + tmap::tm_layout( frame = TRUE, frame.double.line = TRUE, bg.color = "grey85", inner.margins = .08 ) + tmap::tm_title_out( text = 'Obesity Rates in New England', size = 1.2, fontface = 2, fontfamily = "serif" )
The stateMap()
function allows us to create attractive maps very efficiently
and with a lot of flexibility. The function creates highly customized
visualizations through direct inputs and by harnessing the functionality of the
tmap package.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.