Workshop 4

Rory Church

library(tidycensus)
Warning: package 'tidycensus' was built under R version 4.3.3
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(sf)
Warning: package 'sf' was built under R version 4.3.3
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(viridis)
Loading required package: viridisLite
library(scales)

Attaching package: 'scales'
The following object is masked from 'package:viridis':

    viridis_pal

Key: af93d4f0a612d0d83c3f7b9e7fcf784ed32f2240

census_api_key("af93d4f0a612d0d83c3f7b9e7fcf784ed32f2240", install = TRUE, overwrite=TRUE)
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "af93d4f0a612d0d83c3f7b9e7fcf784ed32f2240"
readRenviron("~/.Renviron")

First, I analyze Florida and its Hispanic population:

# Total population: B03003_001
fl_total <- get_acs(
  geography = "county", state = "FL", year = 2023, survey = "acs5",
  variables = "B03003_001"
) |>
  select(GEOID, NAME, total = estimate)
Getting data from the 2019-2023 5-year ACS
# Hispanic population: B03003_003
fl_hisp <- get_acs(
  geography = "county", state = "FL", year = 2023, survey = "acs5",
  variables = "B03003_003"
) |>
  select(GEOID, hisp = estimate)
Getting data from the 2019-2023 5-year ACS
fl_joined <- fl_total |>
  inner_join(fl_hisp, by = "GEOID") |>
  mutate(pct_hispanic = if_else(total > 0, 100 * hisp / total, NA_real_))
fl_geo <- get_acs(
  geography = "county", 
  state = "FL", 
  year = 2023, 
  survey = "acs5",
  variables = "B01003_001", 
  geometry = TRUE
) |>
  select(GEOID, geometry)
Getting data from the 2019-2023 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
fl_map <- fl_geo |>
  left_join(fl_joined, by = "GEOID")
ggplot(fl_map) +
  geom_sf(aes(fill = pct_hispanic), color = NA) +
  scale_fill_viridis(
    option = "C",
    name = "Percent Hispanic",
    labels = label_number(accuracy = 0.1, suffix = "%"),
    na.value = "grey85"
  ) +
  labs(
    title    = "Florida Counties: Percent Hispanic or Latino",
    subtitle = "ACS 2019–2023 (5-year; tidycensus year = 2023)",
    caption  = "Source: U.S. Census Bureau, American Community Survey (via tidycensus)"
  ) +
  theme_void(base_size = 12) +
  theme(plot.title = element_text(face = "bold"))

Larger Hispanic population in South FL and in Mid-Florida around Orlando area.

fl_cnty <- fl_joined |>
  mutate(county = sub(", Florida$", "", NAME))

state_pct <- with(fl_cnty, 100 * sum(hisp, na.rm = TRUE) / sum(total, na.rm = TRUE))

ggplot(fl_cnty, aes(x = pct_hispanic, y = reorder(county, pct_hispanic))) +
  geom_point(size = 2) +
  geom_vline(xintercept = state_pct, linetype = "dashed") +
  scale_x_continuous(labels = label_number(accuracy = 0.1, suffix = "%")) +
  labs(
    x = "Percent Hispanic (ACS 2019–2023)",
    y = NULL,
    title = "Percent Hispanic by County — Florida",
    subtitle = "Dots = counties, ordered low → high; dashed line = statewide percent",
    caption = "Source: U.S. Census Bureau, ACS 5-year via tidycensus"
  ) +
  theme_minimal(base_size = 12)

I also wanted to exmaine North Carolina in my analysis:

# Total population
nc_total <- get_acs(
  geography = "county", state = "NC", year = 2023, survey = "acs5",
  variables = "B02001_001", cache_table = TRUE
) |>
  select(GEOID, NAME, total = estimate)
Getting data from the 2019-2023 5-year ACS
# Black population
nc_black <- get_acs(
  geography = "county", state = "NC", year = 2023, survey = "acs5",
  variables = "B02001_003", cache_table = TRUE
) |>
  select(GEOID, black = estimate)
Getting data from the 2019-2023 5-year ACS
# Join + percentage
nc_joined <- nc_total |>
  inner_join(nc_black, by = "GEOID") |>
  mutate(pct_black = if_else(total > 0, 100 * black / total, NA_real_))
nc_geo <- get_acs(
  geography = "county", state = "NC", year = 2023, survey = "acs5",
  variables = "B01003_001", geometry = TRUE
) |>
  select(GEOID, geometry)
Getting data from the 2019-2023 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
nc_map <- nc_geo |>
  left_join(nc_joined, by = "GEOID")

ggplot(nc_map) +
  geom_sf(aes(fill = pct_black), color = NA) +
  scale_fill_viridis(
    option = "C",
    name = "% Black population",
    labels = label_number(accuracy = 1, suffix = "%"),
    na.value = "grey85"
  ) +
  labs(
    title    = "North Carolina Counties: Percent Black (ACS 2019–2023)",
    subtitle = "5-year ACS; tidycensus year = 2023",
    caption  = "Source: U.S. Census Bureau, American Community Survey (via tidycensus)"
  ) +
  theme_void(base_size = 12) +
  theme(plot.title = element_text(face = "bold"))

Larger Black populations in counties in the Eastern part of the state.

nc_cnty <- nc_joined |>
  mutate(county = sub(", North Carolina$", "", NAME))

state_pct_nc <- with(nc_cnty, 100 * sum(black, na.rm = TRUE) / sum(total, na.rm = TRUE))

ggplot(nc_cnty, aes(x = pct_black, y = reorder(county, pct_black))) +
  geom_point(size = 2) +
  geom_vline(xintercept = state_pct_nc, linetype = "dashed") +
  scale_x_continuous(labels = label_number(accuracy = 0.1, suffix = "%")) +
  labs(
    x = "Percent Black (ACS 2019–2023)",
    y = NULL,
    title = "Percent Black by County — North Carolina",
    subtitle = "Dots = counties, ordered low → high; dashed line = statewide percent",
    caption = "Source: U.S. Census Bureau, ACS 5-year via tidycensus"
  ) +
  theme_minimal(base_size = 12)

# Total population
nc_total_raceinc <- get_acs(
  geography = "county", state = "NC", year = 2023, survey = "acs5",
  variables = "B02001_001"
) |> select(GEOID, NAME, total_pop = estimate)
Getting data from the 2019-2023 5-year ACS
# Black population
nc_black_raceinc <- get_acs(
  geography = "county", state = "NC", year = 2023, survey = "acs5",
  variables = "B02001_003"
) |> select(GEOID, black_pop = estimate)
Getting data from the 2019-2023 5-year ACS
# Median household income
nc_income_raceinc <- get_acs(
  geography = "county", state = "NC", year = 2023, survey = "acs5",
  variables = "B19013_001"
) |> select(GEOID, median_income = estimate)
Getting data from the 2019-2023 5-year ACS
nc_raceinc <- nc_total_raceinc |>
  inner_join(nc_black_raceinc, by = "GEOID") |>
  inner_join(nc_income_raceinc, by = "GEOID") |>
  mutate(pct_black = if_else(total_pop > 0, 100 * black_pop / total_pop, NA_real_))
ggplot(nc_raceinc, aes(x = pct_black, y = median_income)) +
  geom_point(color = "darkblue", size = 2) +
  geom_smooth(method = "lm", se = TRUE, color = "red") +
  labs(
    title = "Percent Black Population vs. Median Household Income",
    subtitle = "North Carolina Counties, ACS 2019–2023",
    x = "% Black population",
    y = "Median household income (USD)",
    caption = "Source: U.S. Census Bureau, American Community Survey"
  ) +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'

Counties with larger Black populations tens to be more low-income. Racial and economic inequalities may overlap in North Carolina.