US Maps Including Alaska and Hawaii

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 and working with FIPS codes are also provided.


CRAN Downloads Build Status codecov

View code used to generate these plots

resources/examples.R

library(usmap)
library(ggplot2)
 
# Blank state map ####
blank_state_map <- plot_usmap()
 
# Blank county map ####
blank_county_map <- plot_usmap("counties")
 
# Population by state ####
state_pop_map <-
  plot_usmap(data = statepop, values = "pop_2015") +
  scale_fill_continuous(low = "white", high = "red", guide = FALSE) +
  scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))
 
# Population by state with labels ####
state_pop_map_labeled <-
  plot_usmap(data = statepop, values = "pop_2015", labels = TRUE) +
  scale_fill_continuous(low = "white", high = "red", guide = FALSE) +
  scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))
 
# Population by county ####
county_pop_map <-
  plot_usmap(data = countypop, values = "pop_2015") +
  scale_fill_continuous(low = "blue", high = "yellow", guide = FALSE) +
  scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))
 
# Poverty percentage by county ####
county_pov_map <-
  plot_usmap(data = countypov, values = "pct_pov_2014") +
  scale_fill_continuous(low = "blue", high = "yellow", guide = FALSE) +
  scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))
 
 
# Combine plots ####
cowplot::plot_grid(
  blank_state_map,
  state_pop_map,
  state_pop_map_labeled,
  blank_county_map,
  county_pop_map,
  county_pov_map,
  nrow = 2
)
 
# Save plots ####
ggsave("resources/example_plots.png", width = 18, height = 10, units = "in")

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 fortified data frame for mapping and visualization. This allows the user to easily add data to color the map.

Shape Files

The shape files that we use to plot the maps in R are located in the data-raw folder. For more information refer to the US Census Bureau. Maps at both the state and county levels are included for convenience (zip code maps may be included in the future).

Installation

To install from CRAN (recommended), run the following code in an R console:

install.packages("usmap")

To install the package from this repository, run the following code in an R console:

# install.package("devtools")
devtools::install_github("pdil/usmap")

Installing using devtools::install_github will provide the most recent developer build of usmap. The developer build may be unstable and not function correctly, use with caution.

To begin using usmap, simply 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("introduction", package = "usmap")
vignette("mapping", package = "usmap")

You can also read the vignettes online at the following links:

Features

  • Obtain map with certain region breakdown
state_map <- us_map(regions = "states")
str(state_map)
#> 'data.frame':    12999 obs. of  9 variables:
#> $ long : num  1091779 1091268 1091140 1090940 1090913 ...
#> $ lat  : num  -1380695 -1376372 -1362998 -1343517 -1341006 ...
#> $ order: int  1 2 3 4 5 6 7 8 9 10 ...
#> $ hole : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
#> $ piece: int  1 1 1 1 1 1 1 1 1 1 ...
#> $ group: chr  "01.1" "01.1" "01.1" "01.1" ...
#> $ fips : chr  "01" "01" "01" "01" ...
#> $ abbr : chr  "AL" "AL" "AL" "AL" ...
#> $ full : chr  "Alabama" "Alabama" "Alabama" "Alabama" ...

county_map <- us_map(regions = "counties")
str(county_map)
#> 'data.frame':    54187 obs. of  10 variables:
#> $ long  : num  1225889 1244873 1244129 1272010 1276797 ...
#> $ lat   : num  -1275020 -1272331 -1267515 -1262889 -1295514 ...
#> $ order : int  1 2 3 4 5 6 7 8 9 10 ...
#> $ hole  : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
#> $ piece : int  1 1 1 1 1 1 1 1 1 1 ...
#> $ group : chr  "01001.1" "01001.1" "01001.1" "01001.1" ...
#> $ fips  : chr  "01001" "01001" "01001" "01001" ...
#> $ abbr  : chr  "AL" "AL" "AL" "AL" ...
#> $ full  : chr  "Alabama" "Alabama" "Alabama" "Alabama" ...
#> $ county: chr  "Autauga County" "Autauga County" "Autauga County" "Autauga County" ...

  • Look up FIPS codes for states and counties
fips("New Jersey")
#> "34"
 
fips(c("AZ", "CA", "New Hampshire"))
#> "04" "06" "33"
 
fips("NJ", county = "Mercer")
#> "34021"
 
fips("NJ", county = c("Bergen", "Hudson", "Mercer"))
#> "34003" "34017" "34021"
  • Retrieve states or counties with FIPS codes
fips_info(c("34", "35"))
#>         full abbr fips
#> 1 New Jersey   NJ   34 
#> 2 New Mexico   NM   35
 
fips_info(c("34021", "35021"))
#>         full abbr         county  fips
#> 1 New Jersey   NJ  Mercer County 34021
#> 2 New Mexico   NM Harding County 35021
  • Plot US maps
plot_usmap("states")
plot_usmap("counties")
  • Display only certain states, counties, or regions
plot_usmap("states", include = .mountain, labels = TRUE)
 
plot_usmap("counties", data = countypov, values = "pct_pov_2014", include = "FL") +
    ggplot2::scale_fill_continuous(low = "green", high = "red", guide = FALSE)
 
plot_usmap("counties", data = countypop, values = "pop_2015", include = .new_england) + 
    ggplot2::scale_fill_continuous(low = "blue", high = "yellow", guide = FALSE)

Acknowledgements

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

News

usmap 0.4.0

New Features

  • Ability to include state abbreviation labels in state maps, see Issue #9.
    • e.g. plot_usmap(labels = TRUE)
  • Add US Census Bureau regional divisions as constants for quick plotting of certain regions.
  • Allow ability to include only certain states while viewing county map, see Issue #11.
    • e.g. us_map("counties", include = "TX") or plot_usmap("counties", include = c("AZ", "NM"))

Improvements

  • Vectorize counties in fips, see Issue #10.
    • e.g. fips("NJ", c("Bergen", "Hudson"))
  • Allow all columns in the data frame that's passed to map_with_data() or plot_usmap() to be preserved.

Bug Fixes

  • Add Kusilvak Census Area (FIPS code 02158), replaces Wade Hampton Census Area (FIPS code 02270).

usmap 0.3.0

Improvements

  • Update shape files to 2017 versions.
  • Improvements to fips and fips_info:
    • Vectorization support (e.g. enter multiple states in fips to receive a vector of corresponding FIPS codes)
    • e.g. fips(c("AK", "AL")) or fips(c("Alaska", "Alabama"))
    • Mixed abbreviations and full names are also supported: fips(c("AK", "Alabama"))
    • Improved warning and error messages.
  • Allow data to be specified by state abbreviation or full name in plot_usmap and map_with_data (instead of just by FIPS code).
    • The data frame passed to plot_usmap or map_with_data (via the data = parameter), can now be a two column data frame with columns "fips" and "values" or "state" and "values".

usmap 0.2.1

Improvements

  • Standardize documentation language.
  • Improve descriptiveness of error messages and warnings.

Bug Fixes

  • Allow data passed to map_with_data and plot_usmap to contain FIPS codes with missing leading zeros.
    • This usually occurs when the codes are read as numeric from a .csv file.

usmap 0.2.0

  • Add map_with_data function for adding user-defined data to map data.
  • Add ability to plot map with data automatically (utilizes new map_with_data function).

usmap 0.1.0

  • First release

Main features

  • Retrieve US map data frame for merging with data and plotting
  • Lookup FIPS codes for states and counties (reverse-lookup as well)
  • Map plotting convenience method (uses ggplot2)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("usmap")

0.5.0 by Paolo Di Lorenzo, 3 months ago


https://dilorenzo.pl/pkgs/usmap


Report a bug at https://github.com/pdil/usmap/issues


Browse source code at https://github.com/cran/usmap


Authors: Paolo Di Lorenzo [aut, cre]


Documentation:   PDF Manual  


GPL-3 | file LICENSE license


Imports utils

Suggests ggplot2, ggrepel, knitr, maptools, proto, rgdal, rmarkdown, scales, sp, stringr, testthat


Suggested by refuge.


See at CRAN