Introduction

The American Community Survey (ACS), conducted by the U.S. Census Bureau, provides demographic, socioeconomic, and housing data that can be used to examine patterns across geographic areas. In this assignment, I use the tidycensus package in R to retrieve and analyze ACS data through both non-spatial and spatial approaches.

The first part of this analysis examines graduate degree attainment across counties in Montana, including a comparison of county estimates and their associated margins of error. The second part explores median household income across census block groups in Maricopa County, Arizona, using spatial ACS data and choropleth mapping.

Part A - Non-Spatial Analysis

library(tidycensus)
library(tidyverse)
library(plotly)
library(mapview)
library(scales)
library(sf)

census_api_key(
  Sys.getenv("CENSUS_API_KEY"),
  install = FALSE
)

Retrieving ACS Data

# 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

Visualizing Margins of Error

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

Interactive visualization of graduate degree attainment estimates across Montana counties.

This plot works well to show the geographic variation in graduate degree attainment across MT counties. The error bars show that some county estimates are much less precise than others. Finally, the horizontal layout makes it easy to compare estimates across all 56 counties. One downside is that overlapping confidence intervals make it difficult to confidently distinguish between some county estimates. There are also a lot of counties in the state so there is some overlap on the y-axis.

Interactive Visualization

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

Part 2 - Spatial Analysis

Retrieve median household income for every census block group in Maricopa County

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

Approximately 5.63% of the census block groups had missing median household income estimates. These missing values may be associated with areas with insufficient household data or areas with few or no residential households.

I chose to retain these missing values rather than remove them or assign them an estimated income. On the map, these areas are displayed in gray to distinguish unavailable data from areas with lower median household incomes.

Create choropleth map

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.

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.