Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created September 16, 2022 21:28
Show Gist options
  • Save walkerke/f31b90ffc24908e424c9e08d6fcd5cad to your computer and use it in GitHub Desktop.
Save walkerke/f31b90ffc24908e424c9e08d6fcd5cad to your computer and use it in GitHub Desktop.
library(tidycensus)
library(tidyverse)
fips_lookup <- fips_codes %>%
distinct(state, state_code)
cd_wfh <- get_acs(
geography = "congressional district",
variables = "DP03_0024P",
year = 2021,
survey = "acs1"
) %>%
mutate(state_code = str_sub(GEOID, 1, 2)) %>%
left_join(fips_lookup, by = "state_code") %>%
mutate(district = paste0(state, "-", str_sub(GEOID, 3, 4))) %>%
mutate(district = str_replace(district, "00", "AL"))
# Source: https://docs.google.com/spreadsheets/d/1XbUXnI9OyfAuhP5P3vWtMuGc5UJlrhXbzZo3AwMuHtk/edit#gid=0
election <- read_csv("~/Downloads/Daily Kos Elections 2012, 2016 & 2020 presidential election results for congressional districts used in 2020 elections - Results.csv", skip = 1)
election20 <- election[,1:5] %>%
set_names(c("district", "incumbent", "party",
"biden", "trump")) %>%
mutate(party = str_remove(party, "[(]"),
party = str_remove(party, "[)]"))
wfh_election <- cd_wfh %>%
select(district, estimate) %>%
inner_join(election20, by = "district")
sysfonts::font.add.google("Roboto", "Roboto")
ggplot(wfh_election, aes(x = biden, y = estimate, color = party)) +
geom_point(size = 3, alpha = 0.7) +
theme_minimal(base_size = 14, base_family = "Roboto") +
scale_color_manual(values = c("blue", "red")) +
labs(x = "% Biden vote share, 2020",
y = "% working from home, 2021 ACS",
color = "Party of congressional\nrepresentative",
caption = "Data sources: 2021 1-year ACS, Daily Kos (election data)\ntidycensus R package | @kyle_e_walker")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment