-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolls.R
63 lines (49 loc) · 2.5 KB
/
polls.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
library(tidyverse)
library(lubridate)
library(xkcd)
library(ggthemes)
library(scales)
poll <- read.table(file="polls.csv",header=T,sep=",")
tail(poll)
### make the plot data to long table first
pollplot <- poll %>% select(Order, Poll,Year, Month, National, Labour, Green, NZFirst) %>%
mutate(GL=Labour + Green, Date=paste0(Year,Month,"15")) %>% select(Order,Date,National,GL,NZFirst) %>%
gather(key=Order,Date)
colnames(pollplot) <- c("Order","Date","Party","Poll")
pollplot <- mutate(pollplot,Company=rep(poll$Poll,3))
pollplot$Date <- as.Date(pollplot$Date,"%Y%B%d")
head(pollplot)
pollW <- poll %>% select(Order, Year, Month, National, Labour, Green, NZFirst) %>%
mutate(GLW=Labour + Green + NZFirst, NW = National + NZFirst, Date=paste0(Year,Month,"15")) %>%
select(Order,Date,NW,GLW) %>% gather(key=Order,Date)
colnames(pollW) <- c("Order","Date","Party","Poll")
pollW <- mutate(pollW,Company=rep(poll$Poll,2))
pollW$Date <- as.Date(pollW$Date,"%Y%B%d")
head(pollW)
# GL + N + W
a <- ggplot(data=pollplot,aes(x=Date,y=Poll,colour=Party)) +
geom_point(position = position_jitter(width=5, height=0.0),aes(shape=Company,size=1.5),alpha=0.7) +
scale_shape_manual(values=c(18,17,15, 16, 1)) +
stat_smooth(formula = y ~ poly(x,5), method="glm", level = 0.99,size=1,aes(weight=Date)) +
scale_y_continuous(name="Percent of the Vote",breaks=seq(0,60,5)) +
scale_color_manual(values=c("brown","blue", "black")) +
theme(axis.text.x = element_text(angle = 45, hjust = 0.6)) +
labs( x = "Polling Date",title ="Going to The Polls",
subtitle = "Guessing the Election",
caption = "Who does Winston 1st Choose") +
theme(plot.subtitle = element_text(size = 15), plot.caption = element_text(size = 15),
axis.title = element_text(size = 15), plot.title = element_text(size = 20))
a
## with winston
w <- ggplot(data=pollW,aes(x=Date,y=Poll,colour=Party)) +
geom_hline(aes(yintercept=50), colour="red", linetype="dashed",size=2)+
stat_smooth(formula = y ~ poly(x,5), method="glm", size=1,aes(weight=Date),level = 0.99) +
geom_point(position = position_jitter(width=5, height=0.0),aes(shape=Company,size=1.5),alpha=0.7) +
scale_shape_manual(values=c(18,17,15, 16, 1)) +
scale_y_continuous(name="Percent of the Vote",breaks=seq(0,60,5)) +
scale_color_manual(values=c("brown","blue")) +
theme(axis.text.x = element_text(angle = 45, hjust = 0.6)) +
labs( x = "Polling Date",title ="Going to The Polls",
subtitle = "Guessing the Election",
caption = "Who does Winston 1st Choose")
w