POS4931: Election Data Science

Using tidycensus, create a map that visualizes a mutation of two census variables (such as creating the percent Hispanic for Florida counties from the 2023 American Community Survey). To create a percentage, you need to divide the Hispanic population by the total population in each county.

  1. Libraries
library(tidycensus)
library(tidyverse)
census_api_key("962c5d6aee465e30a4983ce885560bf1cbbe5375")
  1. Working with the data set
fl <- load_variables(2022, "acs5", cache = TRUE)

fl <- get_acs(geography = "county",
               variables = c(tot_pop = "B01003_001", hispanic = "B03001_002"),
               state = "FL",
               geometry = TRUE,
               year = 2022)
               
fl_wider <- fl|>
  select(-moe) |>
  pivot_wider(names_from = variable, values_from = estimate) |>
                mutate(Hispanic_pct = hispanic/tot_pop)
  1. Graphing
fl_wider |>
  ggplot(aes(fill = Hispanic_pct)) +
  geom_sf(color = NA) +
  scale_fill_viridis_c(option = "magma")