library(RCurl)
library(reshape)
library(htmltab)
library(ggplot2)
library(stringr)
library(scales)
#get the table from the url
theurl <- getURL("https://en.wikipedia.org/wiki/Quebec_general_election,_2014", ssl.verifyPeer=FALSE)
table <- htmltab(theurl, which=12)
df = table[2:nrow(table)-1, c(2, 5, 4, 6, 7)]
names(df) = c("Date", "QLP", "PQ", "CAQ", "QS")
df$Date = as.Date(substr(df[, 1], 9, 18))
for (i in c(2:5)) {
df[[i]] = as.numeric(df[[i]])/100
}
mdata <- melt(df[3:nrow(df)-1, ], id="Date")
names(mdata)[2:3] = c("Party", "Support")
colors = c("#EA6D6A", "#87CEFA", "#9372D4", "#FF8040")
labels = c("QLP", "PQ", "CAQ", "QS")
results = mdata
election = data.frame(Date = rep(df$Date[1], 4),
Support = as.numeric(df[1, 2:5]),
Party = labels)
d = ggplot(results, aes(x=Date, y=Support, colour=Party)) +
geom_point(alpha=0.7) +
geom_smooth(span=0.8, show.legend=F, alpha=0.3) +
scale_colour_manual(values = colors) +
labs(title="Polling during the 2014 Quebec electoral campaign") +
scale_y_continuous(breaks=seq(0,0.5,0.05), minor_breaks=seq(0,0.5,0.01), labels=percent,
limits=c(0, 0.5)) +
scale_x_date(labels=date_format("%b %d"),
breaks=c(seq(as.Date("2014/3/5"), as.Date("2014/4/7"), "5 days"), as.Date("2014/4/7")),
minor_breaks=seq(as.Date("2014/3/5"), as.Date("2014/4/7"), "day"),
limits=c(as.Date("2014/3/5"), as.Date("2014/4/7"))) +
theme(panel.grid.minor=element_line(size=0.2),
panel.grid.major=element_line(size=0.8)) +
geom_point(data=election, size=3, shape=5, show.legend=F) +
geom_point(data=election, size=2, show.legend=F) +
geom_text(data=election, show.legend=F,
aes(label=round(election$Support*100, digits=1)),
size=3, hjust=-0.2, vjust=-0.4, color="#000000")
#save plot as "qc2014.svg"
svg(filename="qc2014.svg",
width=8,
height=5,
pointsize=12,
bg="transparent")
d
dev.off()