Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pdil/usmap
🗺 Create US maps including Alaska and Hawaii in R
https://github.com/pdil/usmap
counties data fips geodata mapping r states usa
Last synced: about 2 hours ago
JSON representation
🗺 Create US maps including Alaska and Hawaii in R
- Host: GitHub
- URL: https://github.com/pdil/usmap
- Owner: pdil
- License: gpl-3.0
- Created: 2015-08-22T14:01:45.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-03-31T22:41:42.000Z (7 months ago)
- Last Synced: 2024-05-01T17:29:45.965Z (6 months ago)
- Topics: counties, data, fips, geodata, mapping, r, states, usa
- Language: R
- Homepage: https://usmap.dev/
- Size: 148 MB
- Stars: 69
- Watchers: 5
- Forks: 16
- Open Issues: 3
-
Metadata Files:
- Readme: README.Rmd
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, include=FALSE}
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")
)
```# 🗺 usmap
[![CRAN](http://www.r-pkg.org/badges/version/usmap?color=blue)](https://cran.r-project.org/package=usmap)
[![Downloads](http://cranlogs.r-pkg.org/badges/grand-total/usmap)](https://cran.r-project.org/package=usmap)
[![check](https://github.com/pdil/usmap/actions/workflows/check.yaml/badge.svg)](https://github.com/pdil/usmap/actions/workflows/check.yaml)
[![codecov](https://codecov.io/gh/pdil/usmap/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pdil/usmap)```{r header, echo=FALSE, fig.height=8}
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
)
```## Purpose
Typically in R it is difficult to create nice US [choropleths](https://en.wikipedia.org/wiki/Choropleth_map) 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`](https://github.com/r-spatial/sf)) object for mapping and visualization. This allows the user to easily add spatial data or features to the US map.
## Shape Files
The shape files that we use to plot the maps in R are located in the [`usmapdata`](https://github.com/pdil/usmapdata) package. These are generated from the [US Census Bureau cartographic boundary files](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.html). Maps at both the state and county levels are included for convenience.
#### Update History
| Date | `usmap` version | Shape File Year | Link |
|------------------|:----------------:|:----------------:|:----------------:|
| January 20, 2024 | 0.7.0 | 2022 | [🔗](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.2022.html) |
| February 27, 2022 | 0.6.0 | 2020 | [🔗](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.2020.html) |
| June 3, 2018 | 0.3.0 | 2017 | [🔗](https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.2017.html) |
| January 29, 2017 | 0.1.0 | 2015 | [🔗](https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.2015.html) |## Installation
📦 To install from CRAN (recommended), run the following code in an R console:
```{r install, eval=FALSE}
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:
```{r install_dev, eval=FALSE}
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:
```{r load}
library(usmap)
```## Documentation
To read the package vignettes, which explain helpful uses of the package, use `vignette`:
```{r vignettes, eval=FALSE}
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](https://github.com/pdil/usmap/issues) or ask a question on Stack Overflow with the [usmap tag](https://stackoverflow.com/questions/tagged/usmap).
## Features
### Map Plots
- Plot US maps
```{r plots, fig.align='center'}
states <- plot_usmap("states")
counties <- plot_usmap("counties")cowplot::plot_grid(states, counties, nrow = 1)
```- Display only certain states, counties, or regions
```{r more_plots}
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)
```- Transform and add spatial data to map
```{r sf_plot}
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)
```### Map Data
- Obtain map data with certain region breakdown
```{r state_map}
us_map(regions = "states")
``````{r county_map}
us_map(regions = "counties")
```### FIPS Codes
- Look up FIPS codes for states and counties
```{r fips}
fips("New Jersey")fips(c("AZ", "CA", "New Hampshire"))
fips("NJ", county = "Mercer")
fips("NJ", county = c("Bergen", "Hudson", "Mercer"))
```- Retrieve states or counties with FIPS codes
```{r fips_info}
fips_info(c("34", "35"))fips_info(c("34021", "35021"))
```- Add FIPS codes to data frame
```{r fips_merge, message=FALSE}
library(dplyr)data <- data.frame(
state = c("NJ", "NJ", "NJ", "PA"),
county = c("Bergen", "Hudson", "Mercer", "Allegheny")
)data %>% rowwise %>% mutate(fips = fips(state, county))
```## 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](https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-line-file.2022.html#list-tab-790442341) which are not copyrighted but do suggest citation. See [section 1.2 of this document](https://www2.census.gov/geo/pdfs/maps-data/data/tiger/tgrshp2022/TGRSHP2022_TechDoc_Ch1.pdf).
If you wish to cite `usmap` in a publication (appreciated but never required!), you may do so in the following way:
```{r citation}
citation("usmap")
```### Coordinate System
`usmap` uses the [US National Atlas Equal Area](https://epsg.io/9311) coordinate system:
sf::st_crs(9311)
```{r crs, echo=FALSE}
sf::st_crs(9311)
```This [coordinate reference system (CRS)](https://www.nceas.ucsb.edu/sites/default/files/2020-04/OverviewCoordinateReferenceSystems.pdf) can also be obtained with `usmap::usmap_crs()`.
## Acknowledgments
The code used to generate the map files was based on this blog post by [Bob Rudis](https://github.com/hrbrmstr): [Moving The Earth (well, Alaska & Hawaii) With R](https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/)