Create a gist now

Instantly share code, notes, and snippets.

anonymous /state_results.R
Created Apr 14, 2016

What would you like to do?
library(rvest)
library(magrittr)
library(stringr)
library(lubridate)
library(tidyr)
library(ggplot2)
library(dplyr)
library(ggthemes)
library(RColorBrewer)
#state = state_nodes[[i]]
state_results = function(state){
state_id = state %>% html_attr("id") %>% str_replace("state", "")
state_name = state %>% html_node(".timeline-header h3 a") %>% html_text() %>% str_trim()
date = state %>% html_nodes(".results-header .timestamp time") %>% html_attr("datetime")
results = state %>% html_nodes(".results-dataset")
allocated = results %>% html_nodes(".results-headings ,pos-1")
results_table = results %>% html_nodes(".results-table") %>% html_table()
#the state's results are available (for at least one party)
if(length(results_table) > 0){
results_meta = results %>% html_nodes(".results-meta h5") %>% html_text()
for(i in 1:length(results_table)){
colnames(results_table[[i]]) = c("Candidate", "VotesPercentage", "Votes", "Delegates")
results_table[[i]]$Candidate = str_trim(str_replace(results_table[[i]]$Candidate, "Winner", ""))
results_table[[i]]$Votes = as.numeric(str_replace_all(results_table[[i]]$Votes, ",", ""))
results_table[[i]]$Delegates = ifelse(is.na(results_table[[i]]$Delegates), 0, results_table[[i]]$Delegates)
results_table[[i]]$State = state_name
results_table[[i]]$Party = results_meta[i]
results_table[[i]]$Date = date[min(i, length(date))]
delegates = allocated[i] %>% html_text() %>% str_trim() %>% str_match("(\\d+)/(\\d+)")
results_table[[i]]$Allocated = delegates[,2]
results_table[[i]]$InPlay = delegates[,3]
}
do.call(rbind, results_table)
}
#state hasnt voted yet
else{
NULL
}
}
results = NULL
html = read_html("http://www.politico.com/2016-election/results/map/president")
state_nodes = html %>% html_nodes(".timeline-group")
for(i in 1:length(state_nodes)){
results = rbind(results, state_results(state_nodes[[i]]))
}
write.csv(results, "state_results.csv", row.names=FALSE)
#setwd("C:/Users/karim/Documents/R/usa_2016")
#results = read.csv("state_results.csv")
results$Date = as.Date(ymd(results$Date))
results$InPlay = as.numeric(results$InPlay)
results_sum = results %>%
group_by(Party, Candidate, Date) %>%
summarise(Delegates = sum(Delegates), InPlay = sum(InPlay)) %>%
mutate(Delegates = cumsum(Delegates)) %>%
mutate(InPlay = cumsum(InPlay))
republican = results_sum %>% filter(Party == "Republican" & max(Delegates) > 10)
rmajority = republican[republican$Candidate=="D. Trump", ]
rmajority$Candidate = "0. Majority"
rmajority$Delegates = 0.5 * rmajority$InPlay
rwinner = republican[republican$Candidate=="D. Trump", ]
rwinner$Candidate = "1. Winner"
rwinner$Delegates = 1237
democrat = results_sum %>% filter(Party == "Democratic" & max(Delegates) > 10)
dmajority = democrat[democrat$Candidate=="H. Clinton", ]
dmajority$Candidate = "Majority"
dmajority$Delegates = 0.5 * dmajority$InPlay
dwinner = democrat[democrat$Candidate=="H. Clinton", ]
dwinner$Candidate = "Winner"
dwinner$Delegates = 2383
rcolors = brewer.pal(4, "Set1")
republican_plot = ggplot(republican, aes(x=Date, y=Delegates))
republican_plot +
theme_wsj() +
theme(axis.text.x = element_text(angle=90)) +
scale_x_date(breaks=republican$Date) +
geom_line(size=1.25, aes(colour=Candidate)) +
geom_line(data=rmajority, aes(col="Majority"), size=1.05, linetype="dashed") +
geom_line(data=rwinner, aes(col="Winning"), size=2) +
scale_color_manual(breaks=c("Winning", "Majority", unique(republican$Candidate)),
limits=c("Winning", "Majority", unique(republican$Candidate)),
values=c("grey55",
"grey75",
rcolors))
dcolors = brewer.pal(3, "Set2")
democrat_plot = ggplot(democrat, aes(x=Date, y=Delegates))
democrat_plot +
theme_wsj() +
theme(axis.text.x = element_text(angle=90)) +
scale_x_date(breaks=democrat$Date) +
geom_line(size=1.25, aes(colour=Candidate)) +
geom_line(data=dmajority, aes(col="Majority"), size=1.05, linetype="dashed") +
geom_line(data=dwinner, aes(col="Winning"), size=2) +
scale_color_manual(breaks=c("Winning", "Majority", unique(democrat$Candidate)),
limits=c("Winning", "Majority", unique(democrat$Candidate)),
values=c("grey55",
"grey75",
dcolors))
#theme(panel.grid.minor=element_blank()) +
#theme(panel.grid.major=element_blank()) +
#theme(plot.margin=unit(c(0.35,0.2,0.30,0.35), "cm"))
#trump = results[str_detect(Candidate, "Trump"),]
#a = aggregate(Delegates ~ Candidate + Date, data=results, sum)
#plot(trump$Date, trump$Delegates, xaxt="n")
#axis.Date(1, at=seq(min(trump$Date), max(trump$Date), by=7), format="%Y-%m-%d")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment