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_001fl_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_003fl_hisp <-get_acs(geography ="county", state ="FL", year =2023, survey ="acs5",variables ="B03003_003") |>select(GEOID, hisp = estimate)
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 populationnc_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 populationnc_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 + percentagenc_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 populationnc_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 populationnc_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 incomenc_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.