Last active
November 19, 2020 23:03
-
-
Save favstats/c569b92aa0906dc3aead1f3eb5242c8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(ggbeeswarm) | |
library(ggthemes) | |
library(glue) | |
elex <- read_csv("https://raw.githubusercontent.com/favstats/USElection2020-NYT-Results/master/data/latest/presidential.csv") | |
elex %>% | |
mutate(votes_perc = votes/sum(votes)) %>% | |
mutate(abs_margin2020 = abs(margin2020)) %>% | |
mutate(vote_size = ifelse(votes >= 100000, "Counties with more than 100k votes", "Counties with less than 100k votes")) %>% | |
mutate(vote_size = fct_relevel(vote_size, c("Counties with more than 100k votes", "Counties with less than 100k votes"))) %>% | |
ggplot(aes(y = margin2020, x= 1, color = margin2020, size = votes_perc, alpha = abs_margin2020)) + | |
geom_quasirandom(method = "tukeyDense", groupOnX=T) + | |
scale_color_gradient2("Margins", | |
low = "#0000ff", | |
mid = "purple", | |
high = "#ff0803") + | |
scale_y_continuous(labels = c("+50% Biden", "0%", "+50% Trump"), | |
breaks = c(-50, 0, 50)) + | |
scale_alpha(range = c(0.4, 1)) + | |
scale_size(range = c(0.65, 12)) + | |
coord_flip() + | |
ggthemes::theme_fivethirtyeight() + | |
geom_hline(yintercept = 0, linetype = "dashed") + | |
labs(y = "\nCounty Margins in the 2020 US Election", caption = glue("\n\nColor reflects by which margin the county was won: blue = greater Biden margin; red = greater Trump margin. The size of the bubble reflects the county share of votes of the national total vote count.\n\nSource: New York Times. Last updated: {as.character(elex$retrieved_time)[1]}. Visualization created by Fabio Votta (@favstats)\n"), title = "US 2020 Presidential Election - Results by County") + | |
facet_wrap(~vote_size, ncol = 1) + | |
theme(legend.position = "none", | |
panel.grid = element_blank(), | |
axis.title.y = element_blank(), | |
axis.text.y = element_blank(), | |
axis.ticks.y = element_blank(), | |
panel.background = element_blank(), plot.caption = element_text(hjust = 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment