Skip to content

Instantly share code, notes, and snippets.

@karlrohe
Created May 1, 2020 14:25
Show Gist options
  • Save karlrohe/a0d57fc539d862feb0098d10cb7979ee to your computer and use it in GitHub Desktop.
Save karlrohe/a0d57fc539d862feb0098d10cb7979ee to your computer and use it in GitHub Desktop.
This plots some presidential approval data over time. This in particular: (VoterDisapprove - VoterApprove) - (AdultDisapprove - AdultApprove)
library(tidyverse)
library(lubridate)
x = read_csv("https://projects.fivethirtyeight.com/trump-approval-data/approval_topline.csv")
x %>%
select(subgroup, approve_estimate, disapprove_estimate, timestamp) %>%
mutate(spreadd = disapprove_estimate - approve_estimate) %>%
filter(subgroup != "All polls") %>%
mutate(date = parse_date_time(timestamp, orders = "H!:M!:S! d! b! Y!")) %>%
mutate(time = date(date)) %>%
group_by(time, subgroup) %>%
summarize(spreadd = mean(spreadd)) %>%
select(subgroup, time, spreadd) %>%
pivot_wider(id_cols = time,
names_from = subgroup,
values_from = spreadd) %>%
mutate(VoterSpreadMinusAdultSpread = Voters - Adults) %>%
ggplot(aes(x=time, y = VoterSpreadMinusAdultSpread)) +
geom_line()+
geom_hline(yintercept = 0, color = "red")+
ggtitle("(VoterDisapprove - VoterApprove) - (AdultDisapprove - AdultApprove)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment