Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created March 23, 2025 18:29
Show Gist options
  • Save walkerke/9bb8df405e534bdb9f67d31bb25abf71 to your computer and use it in GitHub Desktop.
Save walkerke/9bb8df405e534bdb9f67d31bb25abf71 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(tidycensus)
library(showtext)
font_add_google("Montserrat")
showtext_auto()
pres_results <- read_csv("https://raw.githubusercontent.com/tonmcg/US_County_Level_Election_Results_08-24/refs/heads/master/2024_US_County_Level_Presidential_Results.csv") %>%
county_mig <- get_estimates(
geography = "county",
variables = "RDOMESTICMIG",
vintage = 2024
)
merged <- left_join(pres_results, county_mig, by = c("county_fips" = "GEOID")) %>%
mutate(pct_rep = 100 * per_gop,
dem = ifelse(per_dem > per_gop, "More Harris", "More Trump"))
ggplot(merged, aes(x = pct_rep, y = value, color = dem,
size = total_votes)) +
geom_point(alpha = 0.6) +
scale_color_manual(values = c("More Harris" = "blue", "More Trump" = "red")) +
scale_size_area(labels = scales::label_number(big.mark = ","), max_size = 15,
breaks = c(10000, 100000, 1000000, 5000000)) +
theme_minimal(base_family = "Montserrat", base_size = 16) +
scale_x_continuous(labels = scales::percent_format(scale = 1)) +
geom_hline(yintercept = 0, linetype = "dashed") +
guides(color = guide_legend(override.aes = list(size = 4))) +
labs(size = "Total votes cast",
color = "Preferred candidate",
x = "% voting for President Trump, 2024",
y = "Net domestic migration per 1000 residents, 2023-2024",
title = "Domestic Migration and 2024 Presidential Election Results",
subtitle = "US Counties | 2024 Census Population Estimates",
caption = "tidycensus R package | @kyle_e_walker")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment