Skip to content

Instantly share code, notes, and snippets.

@elliottmorris
Created August 31, 2018 15:05
Show Gist options
  • Save elliottmorris/7ef57af37d2e9a55a00fd77da6f88166 to your computer and use it in GitHub Desktop.
Save elliottmorris/7ef57af37d2e9a55a00fd77da6f88166 to your computer and use it in GitHub Desktop.
Graph the democratic margin on the generic ballot
library(tidyverse)
polls_2018 <- read_tsv("http://elections.huffingtonpost.com/pollster/api/v2/questions/18-US-House/poll-responses-clean.tsv") %>%
filter(month(end_date)>=6) %>%
mutate(Dem.Margin = Democrat-Republican) %>%
group_by(mode) %>%
mutate(mean_dem = mean(Dem.Margin))
ggplot(polls_2018,aes(x=Dem.Margin,fill=Dem.Margin>0,col=Dem.Margin>0)) +
geom_bar(width=0.8,alpha=0.5) +
scale_color_manual(values=c("TRUE"="blue","FALSE"="red")) +
scale_fill_manual(values=c("TRUE"="blue","FALSE"="red")) +
facet_wrap(~mode) +
geom_vline(aes(xintercept=mean_dem)) +
geom_text(aes(x=mean_dem+1,y=15,label=sprintf("Mean: D+%s%%",round(mean_dem))),col='black',linetype=2,hjust=0) +
labs(title="Different Polls Tell Different Stories of the 2018 House Midterms",
subtitle="Depending on how you sample Americans, Democrats have a smaller or larger lead in the generic ballot",
x="Democratic Margin (%)",
y= "Number of Polls",caption="Source: Huffington Post Pollster")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment