The urbnmapr
package provides state and county shapefiles that are
compatible to map with ggplot2
.
Shapefiles include Alaska and Hawaii, transformed to be displayed as insets within the continental United States. There are options to include the territories for both the state and county maps.
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
sf
format - Adds various identifiers for merging
- Includes a county-level shapefile and options to add territories
Sign up here to stay up-to-date with R package releases and R resources from the Urban Institute. We will not share your information and we will not email more than once per month.
You can install the latest version of urbnmapr
from GitHub:
# install.packages("devtools")
devtools::install_github("UrbanInstitute/urbnmapr")
The get_urbn_map()
function can be used to call shapefiles. Using the
sf = TRUE
option will load sf
objects. These can be used with
geom_sf()
to create base maps of the continental United States, with
Alaska and Hawaii displayed as insets:
library(tidyverse)
library(urbnmapr)
states_sf <- get_urbn_map("states", sf = TRUE)
states_sf %>%
ggplot(aes()) +
geom_sf(fill = "grey", color = "#ffffff")
library(tidyverse)
library(urbnmapr)
counties_sf <- get_urbn_map("counties", sf = TRUE)
counties_sf %>%
ggplot(aes()) +
geom_sf(fill = "grey", color = "#ffffff")
The default projection is US National Atlas Equal Area.
Maps with US territories can also be called with
get_urbn_map()
.
territories_counties <- get_urbn_map(map = "territories_counties", sf = TRUE)
ggplot() +
geom_sf(territories_counties,
mapping = aes(),
fill = "grey", color = "#ffffff")
Labels for states maps can be accessed with get_urbn_labels()
. Only
state labels are custom, so they can be displayed next to smaller
states. Use the function to call the appropriate labels and then label
the map with geom_sf_text()
.
states_sf <- get_urbn_map(map = "states", sf = TRUE)
states_sf %>%
ggplot() +
geom_sf(aes(),
fill = "grey", color = "#ffffff", size = 0.25) +
geom_sf_text(data = get_urbn_labels(map = "states", sf = TRUE),
aes(label = state_abbv),
size = 3)
The states and counties spatial data 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
.
Continuous data can be mapping on a color scale.
spatial_data <- left_join(get_urbn_map(map = "states", sf = TRUE),
statedata,
by = "state_name")
ggplot() +
geom_sf(spatial_data,
mapping = aes(fill = horate),
color = "#ffffff", size = 0.25) +
labs(fill = "Homeownership rate")
Categorical data can be mapped on a discrete color scale.
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)
county_groups <- countydata %>%
mutate(cat_var = paste0("Group ",
sample(1:4, nrow(countydata), replace = TRUE)))
household_data <- left_join(counties_sf, county_groups, by = "county_fips")
household_data %>%
ggplot() +
geom_sf(mapping = aes(fill = cat_var),
color = NA, size = 0.05) +
labs(fill = "Categorical variable")
library(urbnmapr)
works well with
`library(urbnthemes)
library(urbnthemes)
set_urbn_defaults(style = "map")
## Warning: New theme missing the following elements: axis.ticks.length.x,
## axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y,
## axis.ticks.length.y.left, axis.ticks.length.y.right
states_sf <- get_urbn_map(map = "states", sf = TRUE)
states_sf %>%
left_join(statedata, by = "state_name") %>%
ggplot() +
geom_sf(mapping = aes(fill = horate),
color = "#ffffff", size = 0.25) +
scale_fill_gradientn(labels = scales::percent) +
labs(fill = "Homeownership rate") +
coord_sf(datum = NA)
Map smaller geographies with filter()
. You may need to reproject the
data based on which areas you are mapping.
household_data %>%
filter(state_name == "California") %>%
ggplot() +
geom_sf(mapping = aes(fill = medhhincome),
color = "#ffffff", size = 0.05) +
coord_sf(datum = NA) +
scale_fill_gradientn(labels = scales::dollar) +
labs(fill = "Median household income")
A discrete color scale can also be used for categorical data.
state_categorical <- left_join(get_urbn_map(map = "states", sf = TRUE),
statedata,
by = "state_name") %>%
mutate(cat_var = paste0("Group ",
sample(1:4, nrow(statedata), replace = TRUE)))
ggplot() +
geom_sf(state_categorical, mapping = aes(fill = cat_var),
color = "#ffffff") +
scale_fill_discrete() +
coord_sf(datum = NA) +
labs(fill = "Categorical variable")
Code released under the GNU General Public License v3.0.
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.
urbnmapr's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.