Skip to content

Instantly share code, notes, and snippets.

@elliottmorris
Created April 12, 2020 18:29
Show Gist options
  • Save elliottmorris/d3f6a7b2f1c06c246f4b8fbcc157aecf to your computer and use it in GitHub Desktop.
Save elliottmorris/d3f6a7b2f1c06c246f4b8fbcc157aecf to your computer and use it in GitHub Desktop.
compare rasmussen polling average vs average of all other polls
library(tidyverse)
library(zoo)
trump_approve <- read_csv("https://projects.fivethirtyeight.com/polls-page/president_approval_polls.csv")
trump_approve <- trump_approve %>%
mutate("date" = mdy(end_date)) %>%
dplyr::select(pollster,
date,
"approve" = yes,
"disapprove" = no) %>%
arrange(date) %>%
mutate(Rasmussen = ifelse(pollster == 'Rasmussen Reports/Pulse Opinion Research',approve-disapprove,NA),
Other = ifelse(pollster != 'Rasmussen Reports/Pulse Opinion Research',approve-disapprove,NA))
trump_approve <- trump_approve %>%
group_by(date) %>%
summarise(Rasmussen = mean(Rasmussen,na.rm=T),
Other = mean(Other,na.rm=T)) %>%
mutate(average_other = rollapply(Other,60,FUN=mean,partial=T,na.rm=T),
average_rasmussen = rollapply(Rasmussen,60,FUN=mean,partial=T,na.rm=T))
ggplot(trump_approve, aes(x=date)) +
geom_line(aes(y=average_other,col='Average')) +
geom_line(aes(y=average_rasmussen,col='Rasmussen')) +
labs(x='Date',y='',subtitle="Trump, average approval rating") +
theme_minimal() +
theme(legend.position = 'top',legend.justification = 'left',
panel.grid.minor = element_blank()) +
scale_color_brewer(name='',palette='Dark2')
ggplot(trump_approve, aes(x=date)) +
geom_line(aes(y=average_rasmussen - average_other)) +
labs(x='Date',y='',subtitle="Trump, average approval rating\nRasmussen minus other pollsters") +
scale_y_continuous(breaks=seq(0,20,2)) +
theme_minimal() +
theme(legend.position = 'top',legend.justification = 'left',
panel.grid.minor = element_blank()) +
scale_color_brewer(name='',palette='Dark2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment