| Title: | Thematic Plots for Mazama Spatial Datasets |
|---|---|
| Description: | A suite of convenience functions for generating US state and county thematic maps using datasets from the MazamaSpatialUtils package. |
| Authors: | Jonathan Callahan [aut, cre], Rachel Carroll [aut], Eli Grosman [aut] |
| Maintainer: | Jonathan Callahan <[email protected]> |
| License: | GPL-3 |
| Version: | 0.3.0 |
| Built: | 2026-06-04 06:41:31 UTC |
| Source: | https://github.com/mazamascience/mazamaspatialplots |
Uses the tmap package to generate a thematic map at the
county level. Input consists of a dataframe with countyFIPS identifiers.
Data to plot is specified with parameter argument. If parameter
is mult-valued, mutliple plots will be generated and displayed as "facets".
The returned object is a tmap ggplot object which can be further modified with tmap or ggplot options.
countyMap( data = NULL, parameter = NULL, state_SFDF = "USCensusStates_02", county_SFDF = "USCensusCounties_02", breaks = NULL, palette = "brewer.blues", conusOnly = TRUE, stateCode = NULL, projection = NULL, stateBorderColor = "gray50", countyBorderColor = "white", title = NULL, showLegend = TRUE, legendTitle = NULL, legendOrientation = c("portrait", "landscape"), legendPosition = NULL )countyMap( data = NULL, parameter = NULL, state_SFDF = "USCensusStates_02", county_SFDF = "USCensusCounties_02", breaks = NULL, palette = "brewer.blues", conusOnly = TRUE, stateCode = NULL, projection = NULL, stateBorderColor = "gray50", countyBorderColor = "white", title = NULL, showLegend = TRUE, legendTitle = NULL, legendOrientation = c("portrait", "landscape"), legendPosition = NULL )
data |
Dataframe containing values to plot. This dataframe
must contain a column named |
parameter |
Name of the column in |
state_SFDF |
simple features data frame with US states. It's data
|
county_SFDF |
simple features data frame with US counties. It's data
|
breaks |
Numeric vector of break points. |
palette |
A vector of colors or palette name from the cols4all package
(see |
conusOnly |
Logical specifying Continental US state codes. Ignored when
the |
stateCode |
Vector of state codes to include on the map. |
projection |
Named projection, e.g. "EPSG:4326" or "WGS84" or proj4string. |
stateBorderColor |
Color used for state borders. |
countyBorderColor |
Color used for county borders. |
title |
Vector of text strings to use as individual plot titles. This must be the same length as 'parameter'. |
showLegend |
Logical specifying whether or not to show the legend. |
legendTitle |
Text string to use as the legend title. |
legendOrientation |
Orientation of the legend. Either "portrait" or "landscape". |
legendPosition |
A tm_pos object generated with
|
A ggplot object.
library(MazamaSpatialPlots) mazama_initialize() countyMap( data = example_US_countyCovid, parameter = "cases", breaks = c(0,100,200,500,1000,2000,5000,10000,20000,50000,1e6), title = "COVID-19 Cases on June 01 2020" ) countyMap( data = example_US_countyCovid, parameter = "deaths", state_SFDF = USCensusStates_02, county_SFDF = USCensusCounties_02, breaks = c(0, 1, 50, 100, 250, 500, 1000, 2500, 3000), palette = "brewer.or_rd", stateCode = c( "NY", "PA", "MD", "NJ", "DE"), stateBorderColor = "black", countyBorderColor = 'grey70' ) + tmap::tm_layout( attr.color = 'white', bg.color = "dodgerblue4" ) + tmap::tm_title( text = "COVID-19 Deaths* in the Mid Atlantic", size = 2.0, color = "white", ) + tmap::tm_credits("*as of June 01, 2020", col = "white", position = "left")library(MazamaSpatialPlots) mazama_initialize() countyMap( data = example_US_countyCovid, parameter = "cases", breaks = c(0,100,200,500,1000,2000,5000,10000,20000,50000,1e6), title = "COVID-19 Cases on June 01 2020" ) countyMap( data = example_US_countyCovid, parameter = "deaths", state_SFDF = USCensusStates_02, county_SFDF = USCensusCounties_02, breaks = c(0, 1, 50, 100, 250, 500, 1000, 2500, 3000), palette = "brewer.or_rd", stateCode = c( "NY", "PA", "MD", "NJ", "DE"), stateBorderColor = "black", countyBorderColor = 'grey70' ) + tmap::tm_layout( attr.color = 'white', bg.color = "dodgerblue4" ) + tmap::tm_title( text = "COVID-19 Deaths* in the Mid Atlantic", size = 2.0, color = "white", ) + tmap::tm_credits("*as of June 01, 2020", col = "white", position = "left")
The example_US_countyCovid dataset provides a small
county dataset to use in code examples. The code for creating it demonstrates
creation of a dataest that is compatible with countyMap().
This dataset was generated on 2020-06-12 by running:
library(dplyr)
library(MazamaSpatialUtils)
fileUrl <- "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"
col_names <- c("date", "countyName", "stateName", "countyFIPS", "cases", "deaths")
col_types = "Dcccii"
outputColumns <- c("stateCode", "stateName", "countyFIPS", "countyName", "cases", "deaths")
# After a little trial and error, the following works well:
example_US_countyCovid <-
readr::read_csv(
file = fileUrl,
skip = 1, # Skip the header line
col_names = col_names,
col_types = col_types
) %>%
dplyr::mutate(
stateCode = MazamaSpatialUtils::US_stateNameToCode(stateName),
) %>%
dplyr::filter(.data$date == lubridate::ymd("2020-06-01")) %>%
dplyr::select(!!outputColumns)
save(example_US_countyCovid, file = "data/example_US_countyCovid.rda")
example_US_countyCovidexample_US_countyCovid
A tibble with 52 rows and 3 columns of data.
The example_US_stateObesity dataset provides a small
state dataset to use in code examples. The code for creating it demonstrates
creation of a dataest that is compatible with stateMap().
This dataset was generated on 2020-06-09 by running:
library(dplyr)
library(MazamaSpatialUtils)
fileUrl <- paste0("http://data-lakecountyil.opendata.arcgis.com/datasets/",
"3e0c1eb04e5c48b3be9040b0589d3ccf_8.csv")
col_names <- c("FID", "stateName", "obesityRate", "SHAPE_Length", "SHAPE_Area")
col_types = "icddd"
outputColumns <- c("stateCode", "stateName", "obesityRate")
# After a little trial and error, the following works well:
example_US_stateObesity <-
readr::read_csv(
file = fileUrl,
skip = 1, # Skip the header line
col_names = col_names,
col_types = col_types
) %>%
dplyr::mutate(
stateCode = MazamaSpatialUtils::US_stateNameToCode(stateName)
) %>%
dplyr::select(!!outputColumns)
save(example_US_stateObesity, file = "data/example_US_stateObesity.rda")
example_US_stateObesityexample_US_stateObesity
A tibble with 52 rows and 3 columns of data.
Convenience function to initialize spatial data for US state and county maps. Wraps the following setup lines:
MazamaSpatialUtils::setSpatialDataDir(spatialDataDir)
MazamaSpatialUtils::loadSpatialData("USCensusCounties_02")
MazamaSpatialUtils::loadSpatialData("USCensusStates_02")
mazama_initialize(spatialDataDir = "~/Data/Spatial")mazama_initialize(spatialDataDir = "~/Data/Spatial")
spatialDataDir |
Directory where spatial datasets are found, Default: "~/Data/Spatial" |
No return value.
library(MazamaSpatialPlots) # Set up directory for spatial data spatialDataDir <- tempdir() # typically "~/Data/Spatial" MazamaSpatialUtils::setSpatialDataDir(spatialDataDir) exists("USCensusStates_02") mazama_initialize(spatialDataDir) exists("USCensusStates_02") class(USCensusStates_02)library(MazamaSpatialPlots) # Set up directory for spatial data spatialDataDir <- tempdir() # typically "~/Data/Spatial" MazamaSpatialUtils::setSpatialDataDir(spatialDataDir) exists("USCensusStates_02") mazama_initialize(spatialDataDir) exists("USCensusStates_02") class(USCensusStates_02)
Uses the tmap package to generate a thematic map at the
state level. Input consists of a dataframe with stateCode identifiers.
Data to plot is specified with parameter argument. If parameter
is mult-valued, mutliple plots will be generated and displayed as "facets".
The returned object is a tmap ggplot object which can be further modified with tmap or ggplot options.
stateMap( data = NULL, parameter = NULL, state_SFDF = "USCensusStates_02", breaks = NULL, palette = "brewer.blues", conusOnly = TRUE, stateCode = NULL, projection = NULL, stateBorderColor = "gray50", title = NULL, showLegend = TRUE, legendTitle = NULL, legendOrientation = "portrait", legendPosition = NULL )stateMap( data = NULL, parameter = NULL, state_SFDF = "USCensusStates_02", breaks = NULL, palette = "brewer.blues", conusOnly = TRUE, stateCode = NULL, projection = NULL, stateBorderColor = "gray50", title = NULL, showLegend = TRUE, legendTitle = NULL, legendOrientation = "portrait", legendPosition = NULL )
data |
Dataframe containing values to plot. This dataframe
must contain a column named |
parameter |
Name of the column in |
state_SFDF |
simple features data frame with US states. It's data
|
breaks |
Numeric vector of break points. |
palette |
A vector of colors or palette name from the cols4all package
(see |
conusOnly |
Logical specifying Continental US state codes. Ignored when
the |
stateCode |
Vector of state codes to include on the map. |
projection |
Named projection, e.g. "EPSG:4326" or "WGS84" or proj4string. |
stateBorderColor |
Color used for state borders. |
title |
Vector of text strings to use as individual plot titles. This must be the same length as 'parameter'. |
showLegend |
Logical specifying whether or not to show the legend. |
legendTitle |
Text string to use as the legend title. |
legendOrientation |
Orientation of the legend. Either "portrait" or "landscape". |
legendPosition |
A tm_pos object generated with
|
A ggplot object.
library(MazamaSpatialPlots) stateMap( data = example_US_stateObesity, parameter = "obesityRate", stateBorderColor = "white", title = "2018 Obesity by State" ) # Example of customization using tm_layout and breaks parameter stateMap( data = example_US_stateObesity, parameter = "obesityRate", breaks = seq(20,38,3), stateBorderColor = 'black' ) + tmap::tm_layout( frame = TRUE, frame.double.line = TRUE, bg.color = "grey85", inner.margins = .05 ) + tmap::tm_title( text = 'Obesity Rate by State', size = 1.5, position = tmap::tm_pos_in("center", "top"), fontfamily = "serif" ) # Example using stateCode stateMap( data = example_US_stateObesity, parameter = "obesityRate", stateCode = c('ME', 'NH', 'VT', 'MA', 'RI', 'CT'), stateBorderColor = 'black', legendPosition = tmap::tm_pos_in("right", "bottom") ) + tmap::tm_layout( frame = TRUE, frame.double.line = TRUE, bg.color = "grey85", inner.margins = .08 ) + tmap::tm_title( text = 'Obesity Rates in New England', size = 1.5, fontface = 2, fontfamily = "serif", position = tmap::tm_pos_in("center", "top") )library(MazamaSpatialPlots) stateMap( data = example_US_stateObesity, parameter = "obesityRate", stateBorderColor = "white", title = "2018 Obesity by State" ) # Example of customization using tm_layout and breaks parameter stateMap( data = example_US_stateObesity, parameter = "obesityRate", breaks = seq(20,38,3), stateBorderColor = 'black' ) + tmap::tm_layout( frame = TRUE, frame.double.line = TRUE, bg.color = "grey85", inner.margins = .05 ) + tmap::tm_title( text = 'Obesity Rate by State', size = 1.5, position = tmap::tm_pos_in("center", "top"), fontfamily = "serif" ) # Example using stateCode stateMap( data = example_US_stateObesity, parameter = "obesityRate", stateCode = c('ME', 'NH', 'VT', 'MA', 'RI', 'CT'), stateBorderColor = 'black', legendPosition = tmap::tm_pos_in("right", "bottom") ) + tmap::tm_layout( frame = TRUE, frame.double.line = TRUE, bg.color = "grey85", inner.margins = .08 ) + tmap::tm_title( text = 'Obesity Rates in New England', size = 1.5, fontface = 2, fontfamily = "serif", position = tmap::tm_pos_in("center", "top") )
The USCensusCounties_02 dataset provides a SFDF of US counties to
use in code examples. It is created from converting a US county borders
shapefile to a simple features data frame with additional columns of data. The
code for creating it demonstrates creation of a SFDF that is
compatible with countyMap(). See the MazamaSpatialUtils package for the
function convertUSCensusCounties() that creates this SFDF.
This dataset was generated on 2022-11-07 by running:
library(MazamaSpatialUtils)
setSpatialDataDir("~/Data/Spatial_0.8")
MazamaSpatialUtils::convertUSCensusCounties()
MazamaSpatialUtils::loadSpatialData("USCensusCounties_02")
save(USCensusCounties_02, file = "data/USCensusCounties_02.rda")
USCensusCounties_02USCensusCounties_02
A simple features data frame (SFDF) with 3169 observations and 9 variables.
The USCensusStates_02 dataset provides a SFDF of US states to
use in code examples. It is created by converting a US state borders
shapefile to a simple features data frame with additional columns of data. The
code for creating it demonstrates creation of a SFDF that is
compatible with stateMap(). See the MazamaSpatialUtils package for the
function convertUSCensusStates() that creates this SFDF.
This dataset was generated on 2022-11-007 by running:
library(MazamaSpatialUtils)
setSpatialDataDir("~/Data/Spatial_0.8")
MazamaSpatialUtils::convertUSCensusStates()
MazamaSpatialUtils::loadSpatialData("USCensusStates_02")
save(USCensusStates_02, file = "data/USCensusStates_02.rda")
USCensusStates_02USCensusStates_02
A simple features data frame (SFDF) with 52 observations and 8 variables.
The MazamaSpatialUtils package mus be properly installed and initialized before using functions from the MazamaSpatialPlots package.
This helper function is useful when building automated plot-generation systems.
validateMazamaSpatialUtils()validateMazamaSpatialUtils()
Invisibly returns TRUE if no error message has been generated.