title: “final project” author: “Joshua Hawk” date: “2026-05-01” output: html_document
## `height` was translated to `width`.
## function (data = NULL, mapping = aes(), ..., environment = parent.frame())
## {
## UseMethod("ggplot")
## }
## <bytecode: 0x5c5c86beb640>
## <environment: namespace:ggplot2>
## function (..., orientation = "y")
## {
## deprecate_soft0("4.0.0", "geom_errorbarh()", "geom_errorbar(orientation = \"y\")",
## id = "no-more-errorbarh")
## geom_errorbar(..., orientation = orientation)
## }
## <bytecode: 0x5c5c8c502b78>
## <environment: namespace:ggplot2>
# Install if needed:
# install.packages(c("tidycensus", "tigris", "sf", "dplyr", "ggplot2", "stringr"))
library(tidycensus)
library(tigris)
library(sf)
library(dplyr)
library(ggplot2)
library(stringr)
options(tigris_use_cache = TRUE)
theme_set(theme_minimal())
# ----------------------------------------------------------
# Step 3: Transmit Census API key (uncomment and paste yours)
# ----------------------------------------------------------
census_api_key("d18305720219d4680a63fc284f2f8c8f5bf8bc79")
#------------------------------------------------------------
# 1. Helper: get percent white + MOE for a geography/year
#------------------------------------------------------------
get_pct_white <- function(geo, year, state_fips = "47") {
# B02001_001 = total population
# B02001_002 = white alone
dat <- get_acs(
geography = geo,
state = state_fips,
variables = c(total = "B02001_001", white = "B02001_002"),
year = year,
survey = "acs1",
geometry = FALSE,
output = "wide"
)
dat %>%
mutate(
pct_white = 100 * whiteE / totalE,
pct_white_moe = moe_prop(whiteE, totalE, whiteM, totalM) * 100
)
}
#------------------------------------------------------------
# 2. 2021 TN U.S. House districts map (percent white)
#------------------------------------------------------------
# Data
tn_cd_2021 <- get_pct_white("congressional district", 2021)
# Geometry for 2021 districts
cd_shapes_2021 <- congressional_districts(year = 2021, cb = TRUE) %>%
st_transform(4326) %>%
filter(STATEFP == "47") %>%
left_join(tn_cd_2021, by = c("GEOID" = "GEOID"))
# County borders for overlay
tn_counties <- counties(state = "47", year = 2021, cb = TRUE) %>%
st_transform(4326)
# Breaks to match your legend (adjust if needed)
breaks_2021 <- c(20.50, 67.36, 78.26, 79.78, 85.56, 89.70)
cd_shapes_2021 <- cd_shapes_2021 %>%
mutate(
pct_cat = cut(
pct_white,
breaks = breaks_2021,
include.lowest = TRUE,
right = TRUE
)
)
gg_2021 <- ggplot() +
geom_sf(data = cd_shapes_2021, aes(fill = pct_cat), color = "grey40", size = 0.3) +
geom_sf(data = tn_counties, fill = NA, color = "white", size = 0.2) +
scale_fill_brewer(
name = "Estimated percent white",
palette = "Blues",
drop = FALSE
) +
labs(
title = "U.S. House districts in Tennessee, 2021, shaded by estimated percent white",
subtitle = "ACS 1-year estimates; county borders shown"
) +
theme(
legend.position = "right",
panel.grid.major = element_blank()
)
gg_2021
#------------------------------------------------------------
# 3. 2022 TN U.S. House districts map (percent white)
#------------------------------------------------------------
tn_cd_2022 <- get_pct_white("congressional district", 2022)
cd_shapes_2022 <- congressional_districts(year = 2022, cb = TRUE) %>%
st_transform(4326) %>%
filter(STATEFP == "47") %>%
left_join(tn_cd_2022, by = c("GEOID" = "GEOID"))
tn_counties_2022 <- counties(state = "47", year = 2022, cb = TRUE) %>%
st_transform(4326)
breaks_2022 <- c(26.30, 72.28, 74.52, 79.40, 81.72, 89.70)
cd_shapes_2022 <- cd_shapes_2022 %>%
mutate(
pct_cat = cut(
pct_white,
breaks = breaks_2022,
include.lowest = TRUE,
right = TRUE
)
)
gg_2022 <- ggplot() +
geom_sf(data = cd_shapes_2022, aes(fill = pct_cat), color = "grey40", size = 0.3) +
geom_sf(data = tn_counties_2022, fill = NA, color = "white", size = 0.2) +
scale_fill_brewer(
name = "Estimated percent white",
palette = "Blues",
drop = FALSE
) +
labs(
title = "U.S. House districts in Tennessee, 2022, shaded by estimated percent white",
subtitle = "ACS 1-year estimates; county borders shown"
) +
theme(
legend.position = "right",
panel.grid.major = element_blank()
)
gg_2022
#------------------------------------------------------------
# 4. “Estimates by area” dotplot for TN congressional districts
# (similar to your 116th Congress chart)
#------------------------------------------------------------
# Use 2018 (116th Congress) as an example; change year if needed
tn_cd_2018 <- get_pct_white("congressional district", 2018) %>%
mutate(
name_clean = str_replace(NAME, ", Tennessee", ""),
name_clean = str_replace(name_clean, "Congressional District ", "CD ")
)
gg_dot <- ggplot(tn_cd_2018,
aes(x = pct_white, y = reorder(name_clean, pct_white))) +
geom_errorbarh(
aes(
xmin = pct_white - pct_white_moe,
xmax = pct_white + pct_white_moe
),
height = 0.2,
color = "grey40"
) +
geom_point(size = 2.5, color = "navy") +
labs(
x = "ACS estimate (percent white, with MOE)",
y = NULL,
title = "Estimates by area",
subtitle = "Congressional districts in Tennessee (116th Congress)"
) +
theme(
panel.grid.major.y = element_blank()
)
gg_dot