tidycensus package in R to retrieve and analyze
ACS data through both non-spatial and spatial approaches.library(tidycensus)
library(tidyverse)
library(plotly)
library(mapview)
library(scales)
library(sf)
census_api_key(
Sys.getenv("CENSUS_API_KEY"),
install = FALSE
)
# Use get_acs() to obtain graduate degree by county in Montana
mt_education <- get_acs(
geography = "county",
state = "MT",
variables = "DP02_0066P",
year = 2024,
survey = "acs5"
)
# sort by percentage from highest to lowest
mt_education <- mt_education %>%
arrange(desc(estimate))
# Highest three
mt_education %>%
slice_max(estimate, n = 3) %>%
select(NAME, estimate, moe)
## # A tibble: 3 × 3
## NAME estimate moe
## <chr> <dbl> <dbl>
## 1 Gallatin County, Montana 20.3 1.2
## 2 Missoula County, Montana 18.6 1.2
## 3 Lewis and Clark County, Montana 14.6 1.2
# Sort by percentage from lowest to highest
mt_education <- mt_education %>%
arrange(estimate)
# Lowest three
mt_education %>%
slice_min(estimate, n = 3) %>%
select(NAME, estimate, moe)
## # A tibble: 3 × 3
## NAME estimate moe
## <chr> <dbl> <dbl>
## 1 Fallon County, Montana 2.8 2
## 2 Richland County, Montana 4.1 1.8
## 3 Pondera County, Montana 4.6 1.7
# Create a plot using ggplot() that visualizes percentage of adults with a graduate degree across MT counties
mt_plot_errorbar <- ggplot(mt_education, aes(x = estimate,
y = reorder(NAME, estimate))) +
geom_errorbar(aes(xmin = estimate - moe, xmax = estimate + moe),
width = 0.5, linewidth = 0.5) +
geom_point(color = "darkred", size = 2) +
scale_x_continuous(labels = scales::label_percent(scale = 1)) +
scale_y_discrete(labels = function(x) str_remove(x, " County, Montana|, Montana")) +
labs(title = "Graduate degree attainment, Montana Counties",
subtitle = "2020-2024 ACS",
caption = "Data acquired with R and tidycensus. Error bars represent margin of error around estimates.",
x = "Population with graduate or professional degree (%)",
y = "") +
theme_minimal(base_size = 12)
mt_plot_errorbar
Interactive visualization of graduate degree attainment estimates across Montana counties.
# Convert plot into an interactive chart using ggplotly()
ggplotly(mt_plot_errorbar,
tooltip = "x",
width = 1000,
height = 1400)
Figure 2. Interactive visualization of graduate degree attainment across Montana counties. Hover over the data points to explore the estimates.
# Use load_variables() to find a variable
vars <- load_variables(2024, "acs5")
View(vars)
# Use get_acs() to retrieve spatial data
phoenix_income <- get_acs(
geography = "block group",
variables = "B19013_001",
state = "AZ",
county = "Maricopa",
year = 2024,
geometry = TRUE
)
# Choose basemap
mapview::mapviewOptions(
basemaps = c(
"Esri.WorldGrayCanvas",
"OpenStreetMap",
"Esri.WorldImagery",
"OpenTopoMap"
)
)
mapview(phoenix_income, zcol = "estimate")
Figure 3. Interactive map of median household income across census block groups in Maricopa County, Arizona. Darker and lighter shades represent differences in estimated household income.
phoenix_income_map <- ggplot(phoenix_income) +
geom_sf(aes(fill = estimate),
color = "white", linewidth = 0.1) +
scale_fill_viridis_c(
option = "magma",
direction = -1,
labels = label_dollar()
) +
labs(
title = "Median Household Income ($) in Maricopa County",
subtitle = "2020–2024 ACS 5-Year Estimates by Census Block Group",
caption = paste(
"Source: U.S. Census Bureau, 2020–2024 ACS 5-Year Estimates.",
"Gray areas indicate unavailable income estimates."
),
fill = "Median Household\nIncome",
x = NULL,
y = NULL
) +
coord_sf(expand = FALSE) +
theme(
plot.title = element_text(face = "bold", size = 16),
plot.subtitle = element_text(size = 11, color = "gray40"),
plot.caption = element_text(size = 9, hjust = 0),
plot.margin = margin(15, 20, 15, 15),
legend.position = "right",
panel.grid = element_blank()
)
phoenix_income_map
Figure 4. Median household income across census block groups in Maricopa County, Arizona. Darker purple shades represent higher estimated median household incomes. Gray areas indicate unavailable estimates. Source: U.S. Census Bureau, 2020–2024 ACS 5-Year Estimates.