Skip to content

Instantly share code, notes, and snippets.

@vanatteveldt
Created September 13, 2022 08:57
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 vanatteveldt/b28a04b4aa1599eed85754818e8fc4a4 to your computer and use it in GitHub Desktop.
Save vanatteveldt/b28a04b4aa1599eed85754818e8fc4a4 to your computer and use it in GitHub Desktop.
library(tidyverse)
elections <- dataverse::get_dataframe_by_name(
filename = "1976-2020-president.tab",
dataset = "doi:10.7910/DVN/42MVDX",
server = "dataverse.harvard.edu")
totals = elections |> filter(year == 2020) |> select(state_po, totalvotes) |> unique()
d = elections |>
filter(year %in% c(2016,2020), party_simplified == "DEMOCRAT", candidatevotes > 1000) |>
mutate(percentage =candidatevotes / totalvotes * 100) |>
select(year, state, state_po, percentage) |>
pivot_wider(values_from=percentage, names_from=year) |>
left_join(totals)
ggplot(d, aes(x=`2016`, y=`2020`, size=totalvotes, fill=`2020`, label=state_po)) +
geom_abline(intercept=0, slope=1, lty=2, color="grey") +
geom_label()+
scale_fill_gradient2(midpoint = 50, mid="grey") +
ylab("Biden's vote share in 2020") + xlab("Clinton's vote share in 2016") +
ggthemes::theme_economist() + theme(legend.position = "none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment