The American Community Survey (ACS) is an annual survey run by the U.S. Census Bureau. It gives estimates of income, education, housing, and many other characteristics for places across the country. Because it is a survey of a sample of households, every ACS number is an estimate. Each one comes with a margin of error (MOE) that tells us how much uncertainty surrounds it.
This lab uses the R package tidycensus to download ACS data directly from the Census Bureau and explore it in two parts:
All estimates come from the 2017-2021 ACS 5-year release. The 5-year release combines five years of survey responses, which makes estimates available for small areas such as Census tracts. The workshop materials this lab follows were prepared by Kyle Walker, the author of tidycensus.
We use tidycensus to download data,
tidyverse to manipulate and plot it, sf to
work with map geometry, and plotly, mapview
and leafpop to create interactive graphics.
library(tidycensus) # download ACS data from the Census Bureau API
library(tidyverse) # data manipulation and ggplot2
library(scales) # axis label formatting
library(plotly) # interactive charts
library(sf) # spatial data
library(mapview) # interactive maps
library(leafpop) # popup tables for interactive maps
library(viridisLite) # color palettes
# Download map boundaries fresh instead of saving a copy on this computer
options(tigris_use_cache = FALSE)
get_acs() needs a free Census API key.
The key is stored once on the computer running this report (with
census_api_key(..., install = TRUE)) and is read
automatically, so it does not appear in this document.
The ACS Data Profile variable DP02_0066P is the
percentage of the population aged 25 and over with a graduate or
professional degree. We request it for every county and independent
city in Virginia. (Virginia’s independent cities are counted as county
equivalents by the Census Bureau, so they are included.)
va_grad <- get_acs(
geography = "county",
variables = "DP02_0066P",
state = "VA",
year = 2021
)
## Getting data from the 2017-2021 5-year ACS
## Using the ACS Data Profile
The data have 133 localities and 0 missing estimates. Each row has
the estimate and its margin of error (moe).
With this many localities, a single plot showing all of them is hard to read because the labels overlap. Instead we keep only the 15 localities with the highest shares and the 15 with the lowest shares, and show them in two panels.
va_extremes <- bind_rows(
# 15 highest estimates
va_grad |>
slice_max(estimate, n = 15, with_ties = FALSE) |>
mutate(group = "Highest 15"),
# 15 lowest estimates
va_grad |>
slice_min(estimate, n = 15, with_ties = FALSE) |>
mutate(group = "Lowest 15")
) |>
mutate(
group = factor(group, levels = c("Highest 15", "Lowest 15")),
# Drop the repeated ", Virginia" and " County" text from the labels
locality = str_remove(NAME, " County, Virginia|, Virginia"),
# Text shown when a reader hovers over a point in the interactive chart
hover = sprintf("%s\nEstimate: %.1f%%\nMargin of error: \u00b1%.1f",
locality, estimate, moe)
)
To choose a variable, we search the list of ACS variables with
load_variables(). The 2021 5-year Data Profile tables
contain a variable for the share of all people living below the poverty
level.
# List all ACS 5-year Data Profile variables, then look up the one we want
prof_vars <- load_variables(2021, "acs5/profile")
prof_vars |>
filter(name == "DP03_0128P") |>
select(name, label)
## # A tibble: 1 × 2
## name label
## <chr> <chr>
## 1 DP03_0128P Percent!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE PAS…
We download this variable for every Census tract in Prince William
County and the independent cities of Manassas and Manassas Park (county
codes 153, 683, and 685). Setting geometry = TRUE adds the
tract boundaries so the data can be mapped.
pw_pov <- get_acs(
geography = "tract",
variables = c(poverty_pct = "DP03_0128P"),
state = "VA",
county = c("153", "683", "685"),
year = 2021,
geometry = TRUE
)
There are 103 tracts. 1 of them has no estimate, which we leave as missing rather than filling in a value.
Tract-level estimates can be imprecise because each tract has few residents. As a simple guide to reliability, we flag tracts where the margin of error is larger than 5 percentage points. We also prepare a table of values to show when a reader clicks on a tract on the map.
pw_pov <- pw_pov |>
mutate(wide_moe = !is.na(moe) & moe > 5)
popup_df <- pw_pov |>
st_drop_geometry() |>
transmute(
Tract = str_remove(NAME, ", Prince William County, Virginia|, Manassas.*, Virginia"),
`Poverty rate (%)` = estimate,
`Margin of error (+/- pts)` = moe,
`MOE over 5 points?` = if_else(wide_moe, "Yes", "No")
)
va_plot <- ggplot(va_extremes, aes(x = estimate, y = reorder(locality, estimate))) +
# Error bars show the estimate plus or minus its margin of error
geom_errorbar(aes(xmin = estimate - moe, xmax = estimate + moe),
width = 0.5, linewidth = 0.5) +
# Text for the hover box is only used by the interactive version
geom_point(aes(text = hover), color = "darkred", size = 2) +
# Separate panels for the highest and lowest groups, each with its own axis
facet_wrap(~group, scales = "free", ncol = 1) +
scale_x_continuous(labels = label_percent(scale = 1)) +
labs(
title = "Adults with a graduate or professional degree",
subtitle = "Virginia localities with the highest and lowest shares, 2017-2021 ACS",
caption = "Data acquired with R and tidycensus (DP02_0066P). Error bars represent margin of error.",
x = "ACS estimate (% of population 25+)",
y = ""
) +
theme_minimal(base_size = 12)
The interactive chart below shows each locality as a point with its margin of error as a bar. Hover over a point to see the exact estimate and margin of error.
ggplotly(va_plot, tooltip = "text", height = 700)
Figure 1. Estimated percentage of adults (25 and over) with a graduate or professional degree for the 15 Virginia localities with the highest and the 15 with the lowest shares. Points are ACS estimates and bars are margins of error. The two panels use different axis ranges. Source: U.S. Census Bureau, 2017-2021 ACS 5-year estimates, table DP02, accessed with tidycensus.
What the chart shows. The highest shares are mostly in Northern Virginia and the Charlottesville area. Falls Church city is highest at 49.1%, and the lowest locality is Dickenson County at 2.0%. Falls Church city and Arlington County stand clearly above the rest.
How well does the margin of error plot work? Showing all 133 localities in one plot does not work well because the labels overlap, so we show the extremes instead. The margins of error also affect how the ranking should be read:
The interactive map colors each Census tract by its poverty rate. Clicking on a tract shows its estimate and margin of error. Tracts outlined in cyan are those whose margin of error is larger than 5 percentage points, so the color on those tracts is less certain.
mapview(
pw_pov,
zcol = "estimate",
layer.name = "Percent in poverty<br/>2017-2021 ACS",
col.regions = viridisLite::rocket(n = 100),
map.types = "OpenStreetMap",
popup = popupTable(popup_df, row.numbers = FALSE, feature.id = FALSE)
) +
# Second layer outlines the tracts with a wide margin of error
mapview(
filter(pw_pov, wide_moe),
layer.name = "Margin of error > 5 pts",
map.types = "OpenStreetMap",
color = "cyan",
lwd = 3,
col.regions = "transparent",
alpha.regions = 0,
legend = FALSE,
popup = FALSE
)
Figure 2. Interactive map of the estimated poverty rate by Census tract in Prince William County, Manassas, and Manassas Park, Virginia. Click a tract to see its estimate and margin of error. Cyan outlines mark the 33 tracts with a margin of error over 5 percentage points. Source: U.S. Census Bureau, 2017-2021 ACS 5-year estimates, table DP03.
Poverty rate is a percentage, so a choropleth map (areas shaded by value) is the right choice. A graduated-symbols map would suit raw counts better. Tract estimates are shown in bins, with darker shades meaning higher poverty.
ggplot(pw_pov, aes(fill = estimate)) +
geom_sf(color = "white", linewidth = 0.1) +
theme_void() +
# Binned color scale; direction = -1 makes higher poverty darker
scale_fill_viridis_b(option = "rocket", n.breaks = 6, direction = -1,
na.value = "grey80") +
labs(
title = "Poverty rate by Census tract",
subtitle = "Prince William County, Manassas and Manassas Park, Virginia",
fill = "% below poverty",
caption = "2017-2021 ACS (DP03_0128P) | tidycensus R package | grey = no estimate"
)
Figure 3. Estimated poverty rate by Census tract in Prince William County, Manassas, and Manassas Park, Virginia (2017-2021 ACS). Darker shades show higher poverty. The grey tract has no estimate. Source: U.S. Census Bureau, table DP03, accessed with tidycensus.
What the maps show. Estimated tract poverty rates range from 0% to 19.6%, with a median of 4.6%. Most tracts are in the lower bins, and the higher-poverty tracts are concentrated in a few parts of the study area rather than spread evenly.
One tract (the grey area in the south of the county) has no poverty estimate. This usually happens when a tract has little or no resident population to survey, for example a largely non-residential area such as a military base. We did not investigate the reason further.
tigris package: https://github.com/walkerke/tigris