Usmap
🗺 Create US maps including Alaska, Hawaii, and Puerto Rico in R
Install / Use
/learn @pdil/UsmapREADME
🗺 usmap
<!-- badges: start --> <!-- badges: end --> <img src="man/figures/README-header-1.png" style="display: block; margin: auto;" />Purpose
Typically in R it is difficult to create nice US
choropleths that include
Alaska and Hawaii. The functions presented here attempt to elegantly
solve this problem by manually moving these states to a new location and
providing a simple features (sf)
object for mapping and visualization. This allows the user to easily add
spatial data or features to the US map.
Available Map Data
The map data files that we use to plot the maps in R are located in the
usmapdata package. These are
generated from the US Census Bureau cartographic boundary
files.
Maps at both the state and county levels are included for convenience.
In usmap v0.8.0 the data_year parameter was added to most package
functions, allowing the user to select from multiple available years.
Since data is now stored in usmapdata, updates are no longer tied to
usmap versions.
Update History
| Date | Available Years | |----|:--:| | May 28, 2025 | 2021, 2022, 2023, 2024 |
Prior to usmap v0.8.0:
| Date | usmap version | Available Year |
|----|:--:|:--:|
| May 10, 2024 | 0.7.1 | 2023 |
| January 20, 2024 | 0.7.0 | 2022 |
| February 27, 2022 | 0.6.0 | 2020 |
| June 3, 2018 | 0.3.0 | 2017 |
| January 29, 2017 | 0.1.0 | 2015 |
Installation
📦 To install from CRAN (recommended), run the following code in an R console:
install.packages("usmap")
Developer Build
⚠️ The developer build may be unstable and not function correctly, use with caution.
To install the package from this repository, run the following code in an R console:
install.packages("devtools")
devtools::install_github("pdil/usmap")
This method will provide the most recent developer build of usmap.
To begin using usmap, import the package using the library command:
library(usmap)
Documentation
To read the package vignettes, which explain helpful uses of the
package, use vignette:
vignette(package = "usmap")
vignette("usmap1", package = "usmap") # 1. Introduction
vignette("usmap2", package = "usmap") # 2. Mapping the US
vignette("usmap3", package = "usmap") # 3. Advanced Mapping
For further help with this package, open an issue or ask a question on Stack Overflow with the usmap tag.
Features
Map Plots
- Plot US maps
states <- plot_usmap("states")
counties <- plot_usmap("counties")
cowplot::plot_grid(states, counties, nrow = 1)
<img src="man/figures/README-plots-1.png" style="display: block; margin: auto;" />
- Display only certain states, counties, or regions
library(ggplot2)
mt <- plot_usmap("states", include = .mountain, labels = TRUE)
fl <- plot_usmap("counties", data = countypov, values = "pct_pov_2021", include = "FL", data_year = 2021) +
scale_fill_continuous(low = "green", high = "red", guide = "none")
ne <- plot_usmap("counties", data = countypop, values = "pop_2022", include = .new_england, data_year = 2022) +
scale_fill_continuous(low = "blue", high = "yellow", guide = "none")
cowplot::plot_grid(mt, fl, ne, nrow = 1)
<img src="man/figures/README-more_plots-1.png" style="display: block; margin: auto;" />
- Transform and add spatial data to map
library(ggplot2)
# Transform included `usrivers` data set
rivers_transformed <- usmap_transform(usrivers)
river_map <- plot_usmap("counties", color = "gray80") +
geom_sf(data = rivers_transformed, aes(linewidth = Shape_Length), color = "blue") +
scale_linewidth_continuous(range = c(0.3, 1.5), guide = "none")
# Transform included `earthquakes` data set
earthquakes_above_mag_3 <- earthquakes[earthquakes$mag > 3, ]
eq_transformed <- usmap_transform(earthquakes_above_mag_3)
earthquake_map <- plot_usmap() +
geom_sf(data = eq_transformed, aes(size = mag), color = "red", alpha = 0.25) +
scale_size_continuous(guide = "none")
cowplot::plot_grid(river_map, earthquake_map, nrow = 1)
<img src="man/figures/README-sf_plot-1.png" style="display: block; margin: auto;" />
Map Data
- Obtain map data with certain region breakdown
us_map(regions = "states")
#> Simple feature collection with 52 features and 3 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -2584074 ymin: -2602555 xmax: 2516258 ymax: 731628.1
#> Projected CRS: NAD27 / US National Atlas Equal Area
#> First 10 features:
#> fips abbr full geom
#> 1 02 AK Alaska MULTIPOLYGON (((-2390688 -2...
#> 2 01 AL Alabama MULTIPOLYGON (((1091785 -13...
#> 3 05 AR Arkansas MULTIPOLYGON (((482022.2 -9...
#> 4 04 AZ Arizona MULTIPOLYGON (((-1386064 -1...
#> 5 06 CA California MULTIPOLYGON (((-1716581 -1...
#> 6 08 CO Colorado MULTIPOLYGON (((-787705.6 -...
#> 7 09 CT Connecticut MULTIPOLYGON (((2156162 -83...
#> 8 11 DC District of Columbia MULTIPOLYGON (((1950799 -40...
#> 9 10 DE Delaware MULTIPOLYGON (((2037480 -28...
#> 10 12 FL Florida MULTIPOLYGON (((1853163 -20...
us_map(regions = "counties")
#> Simple feature collection with 3222 features and 4 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -2584074 ymin: -2602555 xmax: 2516258 ymax: 731628.1
#> Projected CRS: NAD27 / US National Atlas Equal Area
#> First 10 features:
#> fips abbr full county
#> 1 02013 AK Alaska Aleutians East Borough
#> 2 02016 AK Alaska Aleutians West Census Area
#> 3 02020 AK Alaska Anchorage Municipality
#> 4 02050 AK Alaska Bethel Census Area
#> 5 02060 AK Alaska Bristol Bay Borough
#> 6 02063 AK Alaska Chugach Census Area
#> 7 02066 AK Alaska Copper River Census Area
#> 8 02068 AK Alaska Denali Borough
#> 9 02070 AK Alaska Dillingham Census Area
#> 10 02090 AK Alaska Fairbanks North Star Borough
#> geom
#> 1 MULTIPOLYGON (((-1757988 -2...
#> 2 MULTIPOLYGON (((-2390688 -2...
#> 3 MULTIPOLYGON (((-1513326 -2...
#> 4 MULTIPOLYGON (((-1899724 -2...
#> 5 MULTIPOLYGON (((-1681144 -2...
#> 6 MULTIPOLYGON (((-1472549 -2...
#> 7 MULTIPOLYGON (((-1452955 -2...
#> 8 MULTIPOLYGON (((-1581283 -1...
#> 9 MULTIPOLYGON (((-1788018 -2...
#> 10 MULTIPOLYGON (((-1508063 -1...
FIPS Codes
- Look up FIPS codes for states and counties
fips("New Jersey")
#> [1] "34"
fips(c("AZ", "CA", "New Hampshire"))
#> [1] "04" "06" "33"
fips("NJ", county = "Mercer")
#> [1] "34021"
fips("NJ", county = c("Bergen", "Hudson", "Mercer"))
#> [1] "34003" "34017" "34021"
- Retrieve states or counties with FIPS codes
fips_info(c("34", "35"))
#> abbr fips full
#> 1 NJ 34 New Jersey
#> 2 NM 35 New Mexico
fips_info(c("34021", "35021"))
#> full abbr county fips
#> 1 New Jersey NJ Mercer County 34021
#> 2 New Mexico NM Harding County 35021
- Add FIPS codes to data frame
library(dplyr)
data <- data.frame(
state = c("NJ", "NJ", "NJ", "PA"),
county = c("Bergen", "Hudson", "Mercer", "Allegheny")
)
data %>% rowwise %>% mutate(fips = fips(state, county))
#> # A tibble: 4 × 3
#> # Rowwise:
#> state county fips
#> <chr> <chr> <chr>
#> 1 NJ Bergen 34003
#> 2 NJ Hudson 34017
#> 3 NJ Mercer 34021
#> 4 PA Allegheny 42003
Additional Information
Citation Information
The images generated by usmap are not under any copyright restrictions
and may be used and distributed freely in any publication or otherwise.
The underlying shapefiles used to generate the map data are derived from the US Census Bureau’s TIGER/Line Shapefiles which are not copyrighted but do suggest citation. See section 1.2 of this document.
If you wish to cite usmap in a publication (appreciated but never
required!), yo
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
