knitr::opts_chunk$set( collapse = TRUE, dpi = 300, comment = "#>", fig.align = "center", fig.path = "man/figures/README-", fig.width = 15, dev.args = list(bg = "transparent") )
library(usmap) library(ggplot2) # City population county map (West Region) #### citypop_t <- usmap_transform(citypop) citypop_t_west <- citypop_t[(citypop_t$abbr %in% .west_region) & (citypop_t$abbr != "AK") & (citypop_t$abbr != "HI"), ] west_county_citypop_map <- plot_usmap("counties", include = .west_region, exclude = c("AK", "HI"), color = "blue", fill = "lightblue") + geom_sf(data = citypop_t_west, aes(size = city_pop), color = "red", alpha = 0.7) + scale_size_continuous(range = c(2, 15), guide = "none") + ggrepel::geom_label_repel( data = citypop_t_west, aes(label = most_populous_city, geometry = geometry), size = 5, alpha = 0.8, label.r = unit(0.5, "lines"), label.size = 0.5, segment.color = "red", segment.size = 1, stat = "sf_coordinates", seed = 1002, max.overlaps = 20 ) # Population by state with labels #### state_pop_map_labeled <- plot_usmap(data = statepop, values = "pop_2022", labels = TRUE) + scale_fill_continuous(low = "white", high = "red", guide = "none") # Blank county map (Alaska) #### ak_county_map <- plot_usmap("counties", include = "AK", color = "red", fill = "#fffdcf") # Poverty by county (South) #### south_pov_map <- plot_usmap("counties", data = countypov, values = "pct_pov_2021", include = .south_region, color = "white", size = 0) + scale_fill_continuous(low = "darkgreen", high = "yellow", guide = "none") # Poverty percentage by county #### county_pov_map <- plot_usmap(data = countypov, values = "pct_pov_2021", size = 0.2) + scale_fill_continuous(low = "blue", high = "yellow", guide = "none") # Rivers map #### rivers_t <- usmap_transform(usrivers) rivers_map <- plot_usmap("counties", color = "gray80") + geom_sf(data = rivers_t, aes(linewidth = Shape_Length), color = "blue") + scale_linewidth_continuous(range = c(0.3, 1.5), guide = "none") # Combine plots #### cowplot::plot_grid( west_county_citypop_map, state_pop_map_labeled, ak_county_map, south_pov_map, county_pov_map, rivers_map, nrow = 2 )
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.
The shape 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.
| Date | usmap
version | Shape File Year | Link |
|------------------|:----------------:|:----------------:|:----------------:|
| 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 | 🔗 |
📦 To install from CRAN (recommended), run the following code in an R console:
install.packages("usmap")
⚠️ 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.package("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)
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.
states <- plot_usmap("states") counties <- plot_usmap("counties") cowplot::plot_grid(states, counties, nrow = 1)
library(ggplot2) mt <- plot_usmap("states", include = .mountain, labels = TRUE) fl <- plot_usmap("counties", data = countypov, values = "pct_pov_2021", include = "FL") + scale_fill_continuous(low = "green", high = "red", guide = "none") ne <- plot_usmap("counties", data = countypop, values = "pop_2022", include = .new_england) + scale_fill_continuous(low = "blue", high = "yellow", guide = "none") cowplot::plot_grid(mt, fl, ne, nrow = 1)
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 eq_transformed <- usmap_transform(earthquakes) 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)
us_map(regions = "states")
us_map(regions = "counties")
fips("New Jersey") fips(c("AZ", "CA", "New Hampshire")) fips("NJ", county = "Mercer") fips("NJ", county = c("Bergen", "Hudson", "Mercer"))
fips_info(c("34", "35")) fips_info(c("34021", "35021"))
library(dplyr) data <- data.frame( state = c("NJ", "NJ", "NJ", "PA"), county = c("Bergen", "Hudson", "Mercer", "Allegheny") ) data %>% rowwise %>% mutate(fips = fips(state, county))
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!), you may do so in the following way:
citation("usmap")
usmap
uses the US National Atlas Equal Area coordinate system:
sf::st_crs(9311)
r
sf::st_crs(9311)
This coordinate reference system (CRS) can also be obtained with usmap::usmap_crs()
.
The code used to generate the map files was based on this blog post by Bob Rudis: Moving The Earth (well, Alaska & Hawaii) With R
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.