This project analyzes the relationship between educational attainment and median household income across all 95 Tennessee counties using data from the 2023 American Community Survey (ACS). The code collects information on county population, median household income, and educational attainment, then combines the datasets into a single file for analysis. The primary goal is to determine whether counties with higher percentages of residents holding bachelor’s degrees or higher tend to have higher median household incomes.
The interactive plot visualizes the same relationship as the scatterplot but allows users to explore the data more closely by hovering over individual data points. This feature helps identify specific counties and compare their educational attainment and income levels.
The interactive graph demonstrates that most counties cluster around the regression line, reinforcing the strong positive correlation found in the statistical analysis. It also makes it easier to identify counties that perform above or below expectations compared to the overall trend.
Overall, the analysis suggests a strong connection between educational attainment and median household income in Tennessee. Counties with higher percentages of residents holding bachelor’s degrees or higher generally experience higher household incomes. The correlation analysis, regression model, scatterplot, interactive visualization, and county map all consistently support the conclusion that education is an important factor associated with economic prosperity across Tennessee counties.
## `geom_smooth()` using formula = 'y ~ x'
###############################################################
# FINAL PROJECT
# Educational Attainment and Median Household Income
# Tennessee Counties
###############################################################
# ==========================
# STEP 1: LOAD PACKAGES
# ==========================
library(tidyverse)
library(tidycensus)
library(scales)
library(plotly)
library(gt)
library(tigris)
library(sf)
# ==========================
# STEP 2: CENSUS API KEY
# ==========================
# Replace with your Census API key
census_api_key("e47500f7fe0e598093bd3bbd282cdfe727627114",
install = FALSE,
overwrite = TRUE)
# ==========================
# STEP 3: DOWNLOAD ACS DATA
# ==========================
year <- 2023
# Total Population
population <- get_acs(
geography = "county",
state = "TN",
variables = c(population = "B01003_001"),
year = year
)
# Median Household Income
income <- get_acs(
geography = "county",
state = "TN",
variables = c(median_income = "B19013_001"),
year = year
)
# Bachelor's Degree or Higher
education <- get_acs(
geography = "county",
state = "TN",
variables = c(bachelors_plus = "B15003_022",
masters = "B15003_023",
professional = "B15003_024",
doctorate = "B15003_025",
total_education = "B15003_001"),
year = year
)
# ==========================
# STEP 4: CLEAN EDUCATION DATA
# ==========================
education_wide <- education %>%
select(GEOID, NAME, variable, estimate) %>%
pivot_wider(
names_from = variable,
values_from = estimate
) %>%
mutate(
bachelors_or_higher =
bachelors_plus +
masters +
professional +
doctorate,
pct_bachelors_plus =
(bachelors_or_higher / total_education) * 100
)
# ==========================
# STEP 6: CLEAN INCOME
# ==========================
income_clean <- income %>%
select(
GEOID,
median_income = estimate
)
# ==========================
# STEP 7: MERGE DATASETS
# ==========================
tn_data <- population %>%
left_join(income_clean,
by = "GEOID") %>%
left_join(
education_wide %>%
select(GEOID,
pct_bachelors_plus),
by = "GEOID"
)
# ==========================
# STEP 8: CREATE CATEGORIES
# ==========================
tn_data <- tn_data %>%
mutate(
income_category =
case_when(
median_income < 55000 ~ "Low",
median_income < 70000 ~ "Medium",
TRUE ~ "High"
),
education_category =
case_when(
pct_bachelors_plus < 20 ~ "Low",
pct_bachelors_plus < 30 ~ "Medium",
TRUE ~ "High"
)
)
# ==========================
# STEP 9: SUMMARY STATISTICS
# ==========================
summary_stats <- tn_data %>%
summarise(
Counties = n(),
Avg_Income =
round(mean(median_income, na.rm = TRUE), 0),
Avg_Education =
round(mean(pct_bachelors_plus, na.rm = TRUE), 1),
Avg_Population =
round(mean(population, na.rm = TRUE), 0)
)
summary_stats
# ==========================
# STEP 10: CORRELATION
# ==========================
correlation <- cor(
tn_data$median_income,
tn_data$pct_bachelors_plus,
use = "complete.obs"
)
correlation
# ==========================
# STEP 11: LINEAR REGRESSION
# ==========================
income_model <- lm(
median_income ~ pct_bachelors_plus,
data = tn_data
)
summary(income_model)
# ==========================
# STEP 12: SCATTERPLOT
# ==========================
scatterplot <- ggplot(
tn_data,
aes(
x = pct_bachelors_plus,
y = median_income
)
) +
geom_point(
color = "steelblue",
size = 3
) +
geom_smooth(
method = "lm",
formula = y ~ x,
color = "red",
se = TRUE
)
labs(
title =
"Educational Attainment and Income in Tennessee Counties",
x =
"Percent with Bachelor's Degree or Higher",
y =
"Median Household Income"
) +
scale_y_continuous(
labels = dollar
) +
theme_minimal()
scatterplot
# ==========================
# STEP 13: INTERACTIVE PLOT
# ==========================
interactive_plot <-
ggplot(
tn_data,
aes(
x = pct_bachelors_plus,
y = median_income,
text = NAME
)
) +
geom_point(
color = "darkgreen",
size = 3
) +
geom_smooth(
method = "lm",
color = "orange"
) +
labs(
title = "Interactive Education vs Income Plot",
x = "Bachelor's Degree or Higher (%)",
y = "Median Household Income"
)
ggplotly(
interactive_plot,
tooltip = "text"
)
# ==========================
# STEP 14: TOP 10 COUNTIES
# ==========================
top_income <- tn_data %>%
arrange(desc(median_income)) %>%
select(
names(tn_data),
median_income,
pct_bachelors_plus
) %>%
head(10)
top_income
# ==========================
# STEP 15: FORMATTED TABLE
# ==========================
# Top 10 counties by median income
top_income_plot <- tn_data %>%
arrange(desc(median_income)) %>%
slice_head(n = 10) %>%
ggplot(
aes(
x = reorder(NAME, median_income),
y = median_income
)
) +
geom_col(fill = "steelblue") +
coord_flip() +
scale_y_continuous(labels = dollar) +
labs(
title = "Top 10 Tennessee Counties by Median Household Income",
x = "County",
y = "Median Household Income"
) +
theme_minimal() +
theme(
plot.title = element_text(face = "bold"),
axis.text.y = element_text(size = 10)
)
top_income_plot
# ==========================
# STEP 16: COUNTY MAP
# ==========================
tn_map <- counties(
state = "TN",
cb = TRUE,
year = year
)
map_data <- tn_map %>%
left_join(
tn_data,
by = "GEOID"
)
county_map <- ggplot(map_data) +
geom_sf(
aes(fill = median_income),
color = "white"
) +
scale_fill_viridis_c(
labels = dollar
) +
labs(
title = "Median Household Income Across Tennessee Counties",
fill = "Income"
) +
theme_minimal()
county_map
# ==========================
# STEP 17: SAVE DATASET
# ==========================
write_csv(
tn_data,
"TN_Education_Income_Project.csv"
)
# ==========================
# STEP 18: EXPORT RESULTS
# ==========================
saveRDS(
income_model,
"income_regression_model.rds"
)
###############################################################
# END OF SCRIPT
###############################################################