Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Created July 24, 2024 21:08
Show Gist options
  • Save JosiahParry/55a2029cb81f0f9a2349cdc061d8dd71 to your computer and use it in GitHub Desktop.
Save JosiahParry/55a2029cb81f0f9a2349cdc061d8dd71 to your computer and use it in GitHub Desktop.
A short script to read in all 2024 polling data from 534 and make a quick visualization.
library(dplyr)
library(tidyr)
library(ggplot2)
url <- "https://projects.fivethirtyeight.com/polls/president-general/2024/national/polls.json"
# read in the raw json path
polls_raw <- RcppSimdJson::fload(url)
# filter to the IDs that contain Harris
has_harris <- polls_raw |>
unnest(answers) |>
filter(choice == "Harris") |>
distinct(id)
# clean up the data and filter to only harris and trump
polls_clean <- polls_raw |>
semi_join(has_harris) |>
unnest(answers) |>
as_tibble() |>
mutate(
across(c(sampleSize, pct), as.numeric),
across(c(created_at, ends_with("Date")), anytime::anydate)
) |>
filter(choice %in% c("Harris", "Trump")) |>
rename_with(heck::to_snek_case)
# make a plot
polls_clean |>
filter(end_date >= "2024-07-20") |>
ggplot(aes(end_date, pct, color = choice)) +
geom_smooth() +
scale_color_viridis_d(direction = -1) +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment