Skip to content

Instantly share code, notes, and snippets.

@ellisp
Created November 6, 2020 06:56
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 ellisp/818cc425b0f17164e1396fd7db18a0bd to your computer and use it in GitHub Desktop.
Save ellisp/818cc425b0f17164e1396fd7db18a0bd to your computer and use it in GitHub Desktop.
Plot of the latest for the votecount for the 2020 US election in selected states
library(extrafont)
library(tidyverse)
library(scales)
myfont <- "Roboto"
main_font <- "Roboto"
heading_font <- "Sarala"
my_theme <- theme_light(base_family = main_font) +
theme(legend.position = "bottom") +
theme(plot.caption = element_text(colour = "grey50"),
strip.text = element_text(size = rel(1), face = "bold"),
plot.title = element_text(family = heading_font))
theme_set(my_theme)
update_geom_defaults("text", list(family = main_font))
update_geom_defaults("label", list(family = main_font))
nyt_data <- read_csv("https://raw.githubusercontent.com/alex/nyt-2020-election-scraper/master/battleground-state-changes.csv")
us_pal = c(Biden = "#1405bd", Trump = "#de0100")
p <- nyt_data %>%
filter(
grepl("Arizona", state) |
grepl("Georgia", state) |
# grepl("Nevada", state) |
grepl("Pennsylvania", state)
) %>%
ggplot(aes(x = timestamp, y = vote_differential, colour = leading_candidate_name)) +
facet_wrap(~state, scales = "free_y", ncol = 2) +
geom_smooth(method = "gam", fullrange = TRUE) +
geom_point() +
geom_hline(yintercept = 0) +
scale_y_continuous(label = comma_format(accuracy = 1)) +
scale_x_datetime(limits = as.POSIXct(c("2020/11/05", "2020/11/07")),
labels = date_format("%a-%d\n%H:%M", tz = "America/New_York" )) +
scale_colour_manual(values = us_pal) +
labs(x = "Time data published (US Eastern Standard Time)",
y = "Number of votes leading by",
colour = "Currently\nleading\ncandidate") +
theme(legend.position = c(0.8, 0.2))
print(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment