Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Created November 6, 2012 20:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jebyrnes/4027380 to your computer and use it in GitHub Desktop.
Save jebyrnes/4027380 to your computer and use it in GitHub Desktop.
MA Senate Projection
#load the excellent methods to query polls
#linked to at http://alandgraf.blogspot.com/2012/11/quick-post-about-getting-and-plotting.html
source("https://raw.github.com/dlinzer/pollstR/master/pollsterAPI.R")
#get the MA Senate Race
datMass <- pollstR(chart="2012-massachusetts-senate-brown-vs-warren",pages="all")
#reshape the data for plotting
library(reshape2)
dm <- melt.data.frame(datMass, id.vars=c("start.date", "N"), measure.vars=c("Warren", "Brown"))
#plot the whole thing and fit a loess curve
qplot(start.date, value, data=dm, size=N, color=variable, group=variable) +
theme_bw() +
scale_color_manual("Candidate", values=c("blue", "red")) +
scale_size_continuous("Sample Size") +
stat_smooth(aes(weight=N)) +
xlab("Poll Start Date") +
ylab("Percentage")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment