Skip to content
State and county maps with Alaska and Hawaii
Branch: master
Clone or download
awunderground Merge pull request #45 from UrbanInstitute/ccdf
Fix serious flaws in get_urbn_maps(map = "ccdf")
Latest commit e42435b Nov 29, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R
README_files
data-raw Fix issues with territories map created ccdf map. Nov 28, 2018
data rework data structuring and clean up for travis Jul 21, 2018
docs Add CODE OF CONDUCT. Aug 25, 2018
man Drop ccdfmap.R. Close #42. Aug 25, 2018
vignettes Add limits to the ccdf map in the vignette. Nov 28, 2018
.Rbuildignore Update .Rbuildignore to include hexsticker and CODE_OF_CONDUCT. Aug 28, 2018
.gitignore
.travis.yml use correct urbnthemes repo Jul 30, 2018
CODE_OF_CONDUCT.md Add CODE OF CONDUCT. Aug 25, 2018
DESCRIPTION update .Rbuildignore for pkgdown, edit DESCRIPTION and travis-ci depe… Jul 30, 2018
NAMESPACE Add get_urbn_labels() and get_urbn_map(). Jun 7, 2018
NEWS.md Update news and documentation. Aug 24, 2018
README.Rmd Add CODE OF CONDUCT. Aug 25, 2018
README.md Add CODE OF CONDUCT. Aug 25, 2018
hexsticker.R Clean up hex stickers and update pkgdown. Aug 25, 2018
urbnmapr.Rproj

README.md

urbnmapr

Travis-CI Build Status

The urbnmapr package provides state and county shapefiles in tibble format that is compatible to map with ggplot2.

Shapefiles include Alaska and Hawaii, transformed to be displayed as insets within the continental United States.

This package is heavily inspired by and derived in part from the fiftystater package by William Murphy. In contrast, urbnmapr:

  • Uses shapefiles from the US Census Bureau
  • Converts the shapefile data to a tibble dataframe
  • Adds various identifiers for merging
  • Includes a county-level shapefile

Installation

You can install the latest version of urbnmapr from GitHub:

# install.packages("devtools")
devtools::install_github("UrbanInstitute/urbnmapr")

Usage

Quick maps

urbnmapr contains two tibble dataframes for creating maps:

  • states
  • counties

The states and counties tibbles can be used with geom_polygon() and coord_map() to create base maps of the continental United States, with Alaska and Hawaii displayed as insets:

library(tidyverse)
library(urbnmapr)

states %>%
  ggplot(aes(long, lat, group = group)) +
  geom_polygon(fill = "grey", color = "#ffffff", size = 0.25) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45)

counties %>%
  ggplot(aes(long, lat, group = group)) +
  geom_polygon(fill = "grey", color = "#ffffff", size = 0.05) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45)

More maps

Additional maps can be accessed with get_urbn_map(). Use the function to create a tibble in your global environment and then map using the same syntax as above.

territories_counties <- get_urbn_map(map = "territories_counties")

territories_counties %>%
  ggplot(aes(long, lat, group = group)) +
  geom_polygon(fill = "grey", color = "#ffffff", size = 0.05) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45)

Labels

Labels for all maps can be accessed with get_urbn_labels(). Use the function to call the appropriate labels and then label the map with geom_text(). Labels can be called inside of geom_text():

states %>%
  ggplot() +
  geom_polygon(aes(long, lat, group = group), 
               fill = "grey", color = "#ffffff", size = 0.25) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  geom_text(data = get_urbn_labels(map = "states"), aes(x = long, lat, label = state_abbv), 
            size = 3)

Or before geom_text() is called:

territories <- get_urbn_map(map = "territories")
labels <- get_urbn_labels(map = "territories")

territories %>%
  ggplot() +
  geom_polygon(aes(long, lat, group = group),
               fill = "grey", color = "#ffffff", size = 0.05) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  geom_text(data = labels, aes(x = long, lat, label = state_abbv), size = 3) 

Merging Data

The states and counties tibbles include various identifiers to simplify merging data. The states states tibble contains state_fips, state_abbv, and state_name. The counties tibble contains county_fips, state_abbv, state_fips, county_name, and state_name. Both tibbles can be piped into ggplot2 to create a choropleth map.

statedata %>% 
  left_join(states, by = "state_name") %>% 
  ggplot(mapping = aes(long, lat, group = group, fill = horate)) +
  geom_polygon(color = "#ffffff", size = .25) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  labs(fill = "Homeownership rate")

household_data <- left_join(countydata, counties, by = "county_fips") 

household_data %>%
  ggplot(aes(long, lat, group = group, fill = medhhincome)) +
    geom_polygon(color = "#ffffff", size = 0.05) +
    coord_map(projection = "albers", lat0 = 39, lat1 = 45)

Styles

library(urbnmapr) works well with the Urban Institute ggplot2 theme.

library(urbnthemes)

set_urbn_defaults(style = "map")
statedata %>% 
  left_join(states, by = "state_name") %>% 
  ggplot(mapping = aes(long, lat, group = group, fill = horate)) +
  geom_polygon(color = "#ffffff", size = .25) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  scale_fill_gradientn(labels = scales::percent) +
  labs(fill = "Homeownership rate")

household_data %>%
  filter(state_name %in% c("Virginia", "Maryland", "District of Columbia")) %>%
  ggplot(aes(long, lat, group = group, fill = medhhincome)) +
    geom_polygon(color = "#ffffff", size = 0.05) +
    coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
    scale_fill_gradientn(labels = scales::dollar) +
  labs(fill = "Median household income")

License

Code released under the GNU General Public License v3.0.

Code of conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

You can’t perform that action at this time.