-
Notifications
You must be signed in to change notification settings - Fork 1
/
polls_dash.Rmd
170 lines (128 loc) · 6.79 KB
/
polls_dash.Rmd
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---
title: "Polls dashboard"
author: "Matt Bixley and Murray Cadzow"
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
---
```{r setup, include=FALSE}
# get required packages
check_package <- function(x){
for( i in x ){
# require returns TRUE invisibly if it was able to load package
if( ! require( i , character.only = TRUE ) ){
# If package was not able to be loaded then re-install
install.packages( i , dependencies = TRUE )
# Load package after installing
require( i , character.only = TRUE )
}
}
}
check_package(c("tidyverse", "lubridate", "flexdashboard", "manipulate"))
# library(tidyverse)
# library(lubridate)
# library(flexdashboard)
# library(manipulate)
poll <- read.table(file="polls.csv",header=T,sep=",")
shape <- c(18,11,15,3,1,17,16) # 7 poll types
# Other at line 101 is Listener and at 108 is Horizon
### make the plot data to long table first
pollplot <- poll %>% mutate(GL=Labour + Green, NACT = National + Act, Date = ymd(paste(Year, Month, 15,sep = '-'))) %>% select(-Order,-Year,-Month) %>% select(Poll,Date, everything()) %>% gather(.,Date, Poll, 3:NCOL(.) )
colnames(pollplot) <- c("Poll","Date","Party","Value")
```
Poll Day
===================================================
Column {data-width=600}
-------------------------------------
### Without Winston
How the major parties are trending
```{r, fig.width=10,fig.height=8.9}
# GL + N + W
pollplot %>% filter(Party %in% c("National","Green","Labour","NZFirst")) %>%
ggplot(.,aes(x=Date,y=Value,colour=Party)) +
geom_point(position = position_jitter(width=5, height=0.0),aes(shape=Poll,size=1.5),alpha=0.5) +
scale_shape_manual(values=shape) +
#stat_smooth(formula = y ~ poly(x,3), method="glm", level = 0.99,size=1,aes(weight=Date)) +
geom_smooth(method="loess",span = 0.2, size=1,level = 0.99) +
scale_y_continuous(name="Percent of the Vote",breaks=seq(0,60,5)) +
scale_color_manual(values=c("green","red","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))
```
Column {data-width=600}
-------------------------------------
### With Winston
What it looks like if we combine Winston 1st with both National and the Labour/Green Block.
**NB:** from August 2017 ACT fixed at 0.7% the approximate value for 1 Seat.
```{r, fig.width=10,fig.height=8.9}
## With Winston
pollplot %>% filter(Party %in% c('NACT',"GL")) %>% left_join(., pollplot %>% filter(Party == "NZFirst"), by = c('Poll',"Date")) %>% mutate(Party = paste0(Party.x,"_" ,Party.y), Value = Value.x + Value.y) %>%
ggplot(data=. ,aes(x=Date,y=Value,colour=Party)) +
scale_y_continuous(name="Percent of the Vote",breaks=seq(0,65,5)) +
geom_hline(aes(yintercept=49), colour="black", linetype="dashed",size=1.5)+
#stat_smooth(formula = y ~ poly(x,3), method="glm", size=1,aes(weight=Date),level = 0.99) +
geom_smooth(span = 0.3, size=1,level = 0.99) +
geom_point(position = position_jitter(width=5, height=0.0),aes(shape=Poll,size=1.5),alpha=0.5) +
scale_shape_manual(values=shape) +
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 = "With Winston") +
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))
```
All Parties
==================================================
Column {data-width=400}
-------------------------------------
### The Changing Vote
```{r, include = FALSE}
library(animation)
for (date in unique(pollplot$Date) ) {
p <- pollplot %>% filter(Party %in% c("National","Green","Labour","NZFirst")) %>% filter(Date == date) %>% group_by(Party) %>% summarise(meanValue = mean(Value, na.rm=TRUE)) %>% ggplot(.,aes(x = Party, y=meanValue,fill=Party)) + geom_bar( stat = "identity") + scale_fill_manual(values=c("green","red","blue", "black")) + ylim(c(0,55)) + ggtitle(as_date(date)) +
theme(plot.title = element_text(size = 40, face = "bold"))
plot(p)
ani.record() # record the current frame
}
date <- pollplot$Date[nrow(pollplot)]
p <- pollplot %>% filter(Party %in% c("National","Green","Labour","NZFirst")) %>% filter(Date == date) %>% group_by(Party) %>% summarise(meanValue = mean(Value, na.rm=TRUE)) %>% ggplot(.,aes(x = Party, y=meanValue,fill=Party)) + geom_bar( stat = "identity") + scale_fill_manual(values=c("green","red","blue", "black")) + ylim(c(0,55)) + ggtitle(as_date(date)) +
theme(plot.title = element_text(size = 40, face = "bold"))
for(i in 1:5){ plot(p)
ani.record() # record the current frame
}
oopts = ani.options(interval = 1.0)
saveGIF(expr = ani.replay(), movie.name = "record_plot.gif" )
```
![](record_plot.gif)
Column {data-width=600}
-------------------------------------
### Major parties
```{r, fig.width = 10}
pollplot %>% filter(Party %in% c("National","Green","Labour","NZFirst")) %>% group_by(Date,Party) %>%
summarise(meanValue = mean(Value, na.rm=TRUE)) %>% ggplot(.,aes(x=Date,y=meanValue,colour=Party)) +
geom_line(alpha=0.7,size=1.5) +
scale_shape_manual(values=shape) +
#stat_smooth(formula = y ~ poly(x,5), method="glm", level = 0.99,size=1,aes(weight=Date)) +
scale_y_continuous(name="Mean Poll Percent of the Vote",breaks=seq(0,60,5)) +
scale_color_manual(values=c("green","red","blue", "black")) +
theme(axis.text.x = element_text(angle = 45, hjust = 0.6)) +
labs( x = "Polling Date",title ="Major Parties") +
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)) + geom_hline(yintercept = 50) + theme_bw()
```
### Minor parties
```{r, fig.width=10}
pollplot %>% filter(!Party %in% c("NACT","National","Green","Labour","NZFirst","GL")) %>% group_by(Date,Party) %>%
summarise(meanValue = mean(Value, na.rm=TRUE)) %>% ggplot(.,aes(x=Date,y=meanValue,colour=Party)) +
geom_line(alpha=0.7,size=1.5) +
scale_shape_manual(values=shape) +
scale_y_continuous(name="Mean Poll Percent of the Vote",breaks=seq(0,60,5)) +
scale_color_manual(values=c("yellow","lightblue","blue", "black","red","purple")) +
theme(axis.text.x = element_text(angle = 45, hjust = 0.6)) +
labs( x = "Polling Date",title ="Minor Parties") +
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)) + theme_bw()
```