Learn R Programming

⚠️There's a newer version (0.7.1) of this package.Take me there.

usmap

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, 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.

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")

Features

  • Obtain map with certain region breakdown
state_map <- us_map(regions = "states")
county_map <- us_map(regions = "counties")
  • Include only certain states
new_england_states <- c("Connecticut", "Maine", "Massachusetts", "New Hampshire", "Rhode Island", "Vermont")
new_england_map <- us_map(regions = "states", include = new_england_states)
  • Look up FIPS codes for states and counties
fips("New Jersey")
# "34"

fips("NJ", county = "Mercer")
# "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
  • Color map with data
plot_usmap(data = statepop, values = "pop_2015", lines = "red") + 
  scale_fill_continuous(name = "Population (2015)", label = scales::comma) + 
  theme(legend.position = "right")

Code for maps shown above

# States 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))

# Counties map
plot_usmap(data = countypop, 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))

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

Copy Link

Version

Install

install.packages('usmap')

Monthly Downloads

5,021

Version

0.2.0

License

GPL-3 | file LICENSE

Maintainer

Paolo Di Lorenzo

Last Published

April 29th, 2017

Functions in usmap (0.2.0)

statepop

Population estimates (2015), state level
statepov

Poverty percentage estimates (2014), state level
map_with_data

Join county or state level data to US map data
plot_usmap

Conveniently plot basic US map
countypov

Poverty percentage estimates (2014), county level
fips

Retrieve FIPS code for either a US state or county
fips_info

Retrieve states or counties using FIPS codes
getFipsInfo

Gets FIPS info for either states or counties depending on input. Helper function for S3 method fips_info.
theme_map

This creates a nice map theme for use in plot_usmap. It is borrowed from the ggthemes package located at this repository: https://github.com/jrnold/ggthemes
us_map

Retrieve US map data
col_classes

Map data column classes
countypop

Population estimates (2015), county level
usmap

usmap: US maps including Alaska and Hawaii