Skip to content

Instantly share code, notes, and snippets.

@walkerke
Last active March 19, 2024 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkerke/a8c6c56347ff3ea17c0c57f2e05bdd16 to your computer and use it in GitHub Desktop.
Save walkerke/a8c6c56347ff3ea17c0c57f2e05bdd16 to your computer and use it in GitHub Desktop.
# Requires tidycensus dev version
# remotes::install_github("walkerke/tidycensus")
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-20/master/2020_US_County_Level_Presidential_Results.csv")
county_mig <- get_estimates(
geography = "county",
variables = "RDOMESTICMIG",
vintage = 2023
)
merged <- left_join(pres_results, county_mig, by = c("county_fips" = "GEOID")) %>%
mutate(pct_dem = 100 * per_dem,
dem = ifelse(per_dem > per_gop, "More Biden", "More Trump"))
ggplot(merged, aes(x = pct_dem, y = value, color = dem,
size = total_votes)) +
geom_point(alpha = 0.6) +
scale_color_manual(values = c("More Biden" = "blue", "More Trump" = "red")) +
scale_size_area(labels = scales::label_number(big.mark = ","), max_size = 10,
breaks = c(10000, 100000, 1000000, 5000000)) +
theme_minimal(base_family = "Montserrat", base_size = 20) +
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 Biden, 2020",
y = "Net domestic migration per 1000 residents, 2022-2023",
title = "Domestic Migration and 2020 Presidential Election Results",
subtitle = "US Counties | 2023 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