Title: | US Maps Including Alaska and Hawaii |
---|---|
Description: | Obtain United States map data frames of varying region types (e.g. county, state). The map data frames include Alaska and Hawaii conveniently placed to the bottom left, as they appear in most maps of the US. Convenience functions for plotting choropleths, visualizing spatial data, and working with FIPS codes are also provided. |
Authors: | Paolo Di Lorenzo [aut, cph, cre] |
Maintainer: | Paolo Di Lorenzo <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.7.1.9000 |
Built: | 2024-11-26 04:37:51 UTC |
Source: | https://github.com/pdil/usmap |
US Census Bureau regional division containing Illinois, Indiana, Michigan, Ohio, and Wisconsin.
.east_north_central
.east_north_central
An object of class character
of length 5.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .east_north_central, labels = TRUE)
plot_usmap(include = .east_north_central, labels = TRUE)
US Census Bureau regional division containing Alabama, Kentucky, Mississippi, and Tennessee.
.east_south_central
.east_south_central
An object of class character
of length 4.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .east_south_central, labels = TRUE)
plot_usmap(include = .east_south_central, labels = TRUE)
US Census Bureau regional division containing New Jersey, New York, and Pennsylvania.
.mid_atlantic
.mid_atlantic
An object of class character
of length 3.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .mid_atlantic, labels = TRUE)
plot_usmap(include = .mid_atlantic, labels = TRUE)
US Census Bureau region containing the East North Central and West North Central divisions. This region was designated as "North Central Region" prior to June 1984.
.midwest_region
.midwest_region
An object of class character
of length 12.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .midwest_region, labels = TRUE)
plot_usmap(include = .midwest_region, labels = TRUE)
US Census Bureau regional division containing Arizona, Colorado, Idaho, Montana, Nevada, New Mexico, Utah, and Wyoming.
.mountain
.mountain
An object of class character
of length 8.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .mountain, labels = TRUE)
plot_usmap(include = .mountain, labels = TRUE)
US Census Bureau regional division containing Connecticut, Maine, Massachusetts, New Hampshire, Rhode Island, and Vermont.
.new_england
.new_england
An object of class character
of length 6.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .new_england, labels = TRUE)
plot_usmap(include = .new_england, labels = TRUE)
Former US Census Bureau region containing the East North Central and West North Central divisions. This region has been designated as "Midwest" since June 1984.
.north_central_region
.north_central_region
An object of class character
of length 12.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .north_central_region, labels = TRUE)
plot_usmap(include = .north_central_region, labels = TRUE)
US Census Bureau region containing the New England and Mid-Atlantic divisions.
.northeast_region
.northeast_region
An object of class character
of length 9.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .northeast_region, labels = TRUE)
plot_usmap(include = .northeast_region, labels = TRUE)
US Census Bureau regional division containing Alaska, California, Hawaii, Oregon, and Washington.
.pacific
.pacific
An object of class character
of length 5.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .pacific, labels = TRUE)
plot_usmap(include = .pacific, labels = TRUE)
US Census Bureau regional division containing Delaware, Florida, Georgia, Maryland, North Carolina, South Carolina, Virginia, District of Columbia, and West Virginia.
.south_atlantic
.south_atlantic
An object of class character
of length 9.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .south_atlantic, labels = TRUE)
plot_usmap(include = .south_atlantic, labels = TRUE)
US Census Bureau region containing the South Atlantic, East South Central, and West South Central divisions.
.south_region
.south_region
An object of class character
of length 17.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .midwest_region, labels = TRUE)
plot_usmap(include = .midwest_region, labels = TRUE)
US Census Bureau regional division containing Iowa, Kansas, Minnesota, Missouri, Nebraska, North Dakota, and South Dakota.
.west_north_central
.west_north_central
An object of class character
of length 7.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .west_north_central, labels = TRUE)
plot_usmap(include = .west_north_central, labels = TRUE)
US Census Bureau region containing the Mountain and Pacific divisions.
.west_region
.west_region
An object of class character
of length 13.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .midwest_region, labels = TRUE)
plot_usmap(include = .midwest_region, labels = TRUE)
US Census Bureau regional division containing Arkansas, Louisiana, Oklahoma, and Texas.
.west_south_central
.west_south_central
An object of class character
of length 4.
See https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
plot_usmap(include = .west_south_central, labels = TRUE)
plot_usmap(include = .west_south_central, labels = TRUE)
The most populous city in each US state, as of the 2010 US Census.
The data is formatted for transforming with usmap_transform()
.
Once the longitude and latitude is transformed, it can be added to
plot_usmap()
using ggplot2::ggplot()
layers.
data(citypop)
data(citypop)
A data frame with 51 rows and 5 variables.
lon
The longitude of the most populous city.
lat
The latitude of the most populous city.
state
The name of the state containing the city.
abbr
The abbreviation of the state containing the city.
most_populous_city
The name of the city.
city_pop
The population of the city.
US census population estimates by county for 2022.
The data is formatted for easy merging with output from us_map()
.
data(countypop)
data(countypop)
A data frame with 3222 rows and 4 variables.
fips
The 5-digit FIPS code corresponding to the county.
abbr
The 2-letter state abbreviation.
county
The full county name.
pop_2022
The 2022 population estimate (in number of people)
for the corresponding county.
US census poverty percentage estimates by county for 2021.
The data is formatted for easy merging with output from us_map()
.
data(countypov)
data(countypov)
A data frame with 3194 rows and 4 variables.
fips
The 5-digit FIPS code corresponding to the county.
abbr
The 2-letter state abbreviation.
county
The full county name.
pct_pov_2021
The 2021 poverty estimate (in percent of county population)
for the corresponding county.
US earthquakes with a magnitude of 2.5 or greater, occurring in the
first half of 2019, from January 1 to June 30, from USGS.
The data is formatted for transforming with usmap_transform()
.
Once the longitude and latitude is transformed, it can be added to
plot_usmap()
using ggplot2::ggplot()
layers.
data(earthquakes)
data(earthquakes)
A data frame with 2254 rows and 3 variables.
lon
The longitude of the earthquake's location.
lat
The latitude of the earthquake's location.
mag
The magnitude of the earthquake.
Each US state and county has a unique FIPS (Federal Information Processing Standards) code. Use this function to obtain the FIPS code for a state or county.
fips(state, county = c())
fips(state, county = c())
state |
The state(s) for which to obtain a FIPS code(s). Can be entered as either a state abbreviation or full name (case-insensitive).
|
county |
The county for which to obtain a FIPS code. Can be entered with or without "county" (case-insensitive). |
State and county FIPS (Federal Information Processing Standards) are two and five digit codes, respectively. They uniquely identify all states and counties within the United States. The first two digits of the five digit county codes correspond to the state that the county belongs to. FIPS codes also exist for US territories and minor outlying islands, though this package only provides information for the 50 US states (and their associated counties and census designated areas).
The FIPS code(s) of given state
or county
.
If only states are entered, a vector of length equal to the number of states
is returned. If any states are not found or are invalid, NA
is returned in their place.
If a state and county are entered, a single value with the FIPS code for the given county is returned. If the county is invalid for the given state, an error is thrown.
If both state
and county
are omitted, the entire list of available FIPS
codes is returned, sorted by the state's abbreviation (e.g. Alaska (AK) comes
before Alabama (AL)).
A state
must be included when searching for county
,
otherwise multiple results may be returned for duplicate county names.
fips() fips("NJ") fips("California") fips(c("AK", "CA", "UT")) fips("CA", county = "orange") fips(state = "AL", county = "autauga") fips(state = "Alabama", county = "Autauga County")
fips() fips("NJ") fips("California") fips(c("AK", "CA", "UT")) fips("CA", county = "orange") fips(state = "AL", county = "autauga") fips(state = "Alabama", county = "Autauga County")
Retrieve states or counties using FIPS codes
fips_info(fips, sortAndRemoveDuplicates = FALSE) ## S3 method for class 'numeric' fips_info(fips, sortAndRemoveDuplicates = FALSE) ## S3 method for class 'character' fips_info(fips, sortAndRemoveDuplicates = FALSE)
fips_info(fips, sortAndRemoveDuplicates = FALSE) ## S3 method for class 'numeric' fips_info(fips, sortAndRemoveDuplicates = FALSE) ## S3 method for class 'character' fips_info(fips, sortAndRemoveDuplicates = FALSE)
fips |
A one to five digit, either |
sortAndRemoveDuplicates |
Whether or not to sort the output and remove
duplicates. By default, the output will be returned in the order of
the values provided to the |
A data frame with the states or counties and the associated FIPS codes.
If fips
is omitted, the data frame containing all available states is
returned.
fips_info(2) fips_info("2") fips_info(c("02", "03", "04")) fips_info(2016) fips_info(c("02016", "02017"), sortAndRemoveDuplicates = TRUE)
fips_info(2) fips_info("2") fips_info(c("02", "03", "04")) fips_info(2016) fips_info(c("02016", "02017"), sortAndRemoveDuplicates = TRUE)
Join county or state level data to US map data
map_with_data(data, values = "values", include = c(), exclude = c(), na = NA)
map_with_data(data, values = "values", include = c(), exclude = c(), na = NA)
data |
The data that should be joined to a US map. This
parameter should be a data frame consisting of two columns,
a fips code (2 characters for state, 5 characters for county)
and the value that should be associated with that region. The
columns of |
values |
The name of the column that contains the values to be associated
with a given region. The default is |
include |
The regions to include in the resulting map. If |
exclude |
The regions to exclude in the resulting map. If |
na |
The value to be inserted for states or counties that don't have
a value in |
A data frame composed of the map data frame (from us_map()
) except
an extra column containing the values in data
is included.
The result can be plotted using ggplot2::ggplot()
or plot_usmap()
.
state_data <- data.frame(fips = c("01", "02", "04"), values = c(1, 5, 8)) df <- map_with_data(state_data, na = 0) state_data <- data.frame(state = c("AK", "CA", "Utah"), values = c(6, 9, 3)) df <- map_with_data(state_data, na = 0)
state_data <- data.frame(fips = c("01", "02", "04"), values = c(1, 5, 8)) df <- map_with_data(state_data, na = 0) state_data <- data.frame(state = c("AK", "CA", "Utah"), values = c(6, 9, 3)) df <- map_with_data(state_data, na = 0)
Conveniently plot basic US map
plot_usmap( regions = c("states", "state", "counties", "county"), include = c(), exclude = c(), data = data.frame(), values = "values", theme = theme_map(), labels = FALSE, label_color = "black", ... )
plot_usmap( regions = c("states", "state", "counties", "county"), include = c(), exclude = c(), data = data.frame(), values = "values", theme = theme_map(), labels = FALSE, label_color = "black", ... )
regions |
The region breakdown for the map, can be one of
( |
include |
The regions to include in the resulting map. If |
exclude |
The regions to exclude in the resulting map. If |
data |
A data frame containing values to plot on the map. This
parameter should be a data frame consisting of two columns,
a FIPS code (2 characters for state, 5 characters for county)
and the value that should be associated with that region. The
columns of |
values |
The name of the column that contains the values to be associated
with a given region. The default is |
theme |
The theme that should be used for plotting the map. The default
is |
labels |
Whether or not to display labels on the map. Labels are not displayed by default. |
label_color |
The color of the labels to display. Corresponds to the |
... |
Other arguments to pass to |
A ggplot2::ggplot object that contains a basic
US map with the described parameters. Since the result is a ggplot
object, it can be extended with more ggplot2::Geom layers, scales, labels,
themes, etc.
plot_usmap() plot_usmap(regions = "states") plot_usmap(regions = "counties") plot_usmap(regions = "state") plot_usmap(regions = "county") # Output is ggplot object so it can be extended # with any number of ggplot layers library(ggplot2) plot_usmap(include = c("CA", "NV", "ID", "OR", "WA")) + labs(title = "Western States") # Color maps with data plot_usmap(data = statepop, values = "pop_2022") # Include labels on map (e.g. state abbreviations) plot_usmap(data = statepop, values = "pop_2022", labels = TRUE) # Choose color for labels plot_usmap(data = statepop, values = "pop_2022", labels = TRUE, label_color = "white")
plot_usmap() plot_usmap(regions = "states") plot_usmap(regions = "counties") plot_usmap(regions = "state") plot_usmap(regions = "county") # Output is ggplot object so it can be extended # with any number of ggplot layers library(ggplot2) plot_usmap(include = c("CA", "NV", "ID", "OR", "WA")) + labs(title = "Western States") # Color maps with data plot_usmap(data = statepop, values = "pop_2022") # Include labels on map (e.g. state abbreviations) plot_usmap(data = statepop, values = "pop_2022", labels = TRUE) # Choose color for labels plot_usmap(data = statepop, values = "pop_2022", labels = TRUE, label_color = "white")
US census population estimates by state for 2022.
The data is formatted for easy merging with output from us_map()
.
data(statepop)
data(statepop)
A data frame with 52 rows and 4 variables.
fips
The 2-digit FIPS code corresponding to the state.
abbr
The 2-letter state abbreviation.
full
The full state name.
pop_2022
The 2022 population estimate (in number of people)
for the corresponding state.
US census poverty percentage estimates by state for 2021.
The data is formatted for easy merging with output from us_map()
.
data(statepov)
data(statepov)
A data frame with 51 rows and 4 variables.
fips
The 2-digit FIPS code corresponding to the state.
abbr
The 2-letter state abbreviation.
full
The full state name.
pct_pov_2021
The 2021 poverty estimate (in percent of state population)
for the corresponding state
Retrieve US map data
us_map( regions = c("states", "state", "counties", "county"), include = c(), exclude = c() )
us_map( regions = c("states", "state", "counties", "county"), include = c(), exclude = c() )
regions |
The region breakdown for the map, can be one of
( |
include |
The regions to include in the resulting map. If |
exclude |
The regions to exclude in the resulting map. If |
A data frame of US map coordinates divided by the desired regions
.
usmapdata::us_map()
of which this function is a wrapper for.
str(us_map()) df <- us_map(regions = "counties") west_coast <- us_map(include = c("CA", "OR", "WA")) south_atl_excl_FL <- us_map(include = .south_atlantic, exclude = "FL")
str(us_map()) df <- us_map(regions = "counties") west_coast <- us_map(include = c("CA", "OR", "WA")) south_atl_excl_FL <- us_map(include = .south_atlantic, exclude = "FL")
It is usually difficult or inconvenient to create US maps that include both Alaska and Hawaii in a convenient spot. All map data presented in this package uses the US National Atlas Equal Area projection.
Alaska and Hawaii have been manually moved to a new location so that
their new coordinates place them to the bottom-left corner of
the map. These maps can be accessed by using the us_map()
function.
The function provides the ability to retrieve maps with either
state borders or county borders using the regions
parameter
for convenience.
States (or counties) can be included and excluded using the provided
include
and exclude
parameters. These parameters can be used
together with any combination of names, abbreviations, or FIPS code to
create more complex maps.
Several functions have been included to lookup the US state or county pertaining to a FIPS code.
Likewise a reverse lookup can be done where a FIPS code can be used to retrieve the associated states or counties. This can be useful when preparing data to be merged with the map data frame.
A convenience function plot_usmap()
has been included which
takes similar parameters to us_map()
and returns a ggplot2::ggplot2
object. Since the output is a ggplot
object, other layers can be
added such as scales, themes, and labels. Including data in the function call
will color the map according to the values in the data, creating a choropleth.
It is also possible to add spatial data to the map, in the form of either
data frames or simple features (sf::sf) objects. If necessary, the
data can be transformed to be in the same coordinate reference system as
usmap by using usmap_transform()
and then plotted using ggplot2::geom_sf()
.
Paolo Di Lorenzo
Email: dilorenzo@hey
GitHub: https://github.com/pdil/
Rudis, Bob. "Moving The Earth (well, Alaska & Hawaii) With R." Blog post. Rud.is., 16 Nov. 2014. Web. 10 Aug. 2015. https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/.
Helpful links:
FIPS code information
https://en.wikipedia.org/wiki/FIPS_county_code
https://en.wikipedia.org/wiki/FIPS_state_code
US Census Shapefiles
https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.html
Map Features
https://en.wikipedia.org/wiki/Map_projection
https://en.wikipedia.org/wiki/Equal-area_projection
https://en.wikipedia.org/wiki/Choropleth
https://epsg.io/9311 (US National Atlas Equal Area)
This coordinate reference system (CRS) represents
the canonical projection used by the usmap
package. It can
be used to transform shape files, spatial points, spatial data
frames, etc. to the same coordinate representation that is used
by the plot_usmap
function.
usmap_crs()
usmap_crs()
Converting a spatial object of map coordinates will allow those points to line up with the regular usmap plot by applying the same US National Atlas Equal Area projection (including Alaska and Hawaii of course) to those points as well.
The input data
is assumed to contain longitude and latitude coordinates
by default. If this is not the case, provide an sf::st_crs object
to the crs
parameter with the appropriate coordinate reference system.
usmap_transform(data, ...) ## S3 method for class 'sf' usmap_transform(data, ...) ## S3 method for class 'data.frame' usmap_transform(data, ..., input_names = c("lon", "lat"), output_names = NULL)
usmap_transform(data, ...) ## S3 method for class 'sf' usmap_transform(data, ...) ## S3 method for class 'data.frame' usmap_transform(data, ..., input_names = c("lon", "lat"), output_names = NULL)
data |
A data frame containing coordinates in a two column format where the first column represents longitude and the second data frame represents latitude. The names of the data frame column do not matter, just that the order of the columns is kept intact. |
... |
Additional parameters passed onto sf::st_as_sf.
By default, |
input_names |
A character vector of length two which specifies the
longitude and latitude columns of the input data (the ones that should be
transformed), respectively. Only required if the input data is
a |
output_names |
Defunct, this parameter is no longer used. The output
of this function will have a column named |
An sf
object containing the transformed coordinates from the
input data frame with the US National Atlas Equal Area projection applied.
The transformed columns will be appended to the data frame so that all
original columns should remain intact.
data <- data.frame( lon = c(-74.01, -95.36, -118.24, -87.65, -134.42, -157.86), lat = c(40.71, 29.76, 34.05, 41.85, 58.30, 21.31), pop = c(8398748, 2325502, 3990456, 2705994, 32113, 347397) ) # Transform data transformed_data <- usmap_transform(data) # Plot transformed data on map library(ggplot2) plot_usmap() + geom_sf( data = transformed_data, aes(size = pop), color = "red", alpha = 0.5 )
data <- data.frame( lon = c(-74.01, -95.36, -118.24, -87.65, -134.42, -157.86), lat = c(40.71, 29.76, 34.05, 41.85, 58.30, 21.31), pop = c(8398748, 2325502, 3990456, 2705994, 32113, 347397) ) # Transform data transformed_data <- usmap_transform(data) # Plot transformed data on map library(ggplot2) plot_usmap() + geom_sf( data = transformed_data, aes(size = pop), color = "red", alpha = 0.5 )
Major rivers in the United States.
The data is can be transformed with usmap_transform()
.
Once the Shape
strings are transformed, it can be added to
plot_usmap()
using a ggplot2::geom_sf()
layer.
data(usrivers)
data(usrivers)
A simple features (sf) data frame with 55 rows and 5 variables.
NAME
The name of the river.
SYSTEM
The system the river belongs to.
MILES
The length of the river in miles.
Shape_Length
The length of the river in the coordinate system.
Shape
The MULTILINESTRING features depicting the river, for plotting.
https://www.arcgis.com/home/item.html?id=290e4ab8a07f4d2c8392848d011add32#!
Sources: Esri; Rand McNally; Bartholemew and Times Books; Digital Chart of the World (DCW), U.S. National Geospatial-Intelligence Agency (NGA); i-cubed