generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-gop.Rmd
128 lines (122 loc) · 5.75 KB
/
02-gop.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
# Project Republicans
<p>
Everything about the 2024 republican presidential primary which is the former president Trump aganist the field of DeSantis, Haley and Ramaswamy.
</P>
## National Polling
<p>
Trump with a commanding lead against who ever pops-up to second place. All the polls are sourced from <code>538.com</code>, only non-partisan polls which means they are not sponsored by the candidates or their allies. The polls are calculated on a 7-day average and the trendlines are fitted with **Loess** regression curves with a 0.5 span.
</p>
```{r ,gop_primary,echo = FALSE, results = 'hide', warning = FALSE,message=FALSE, fig.height=5,fig.width=10,fig.align='center', dpi=900}
library(ggplot2)
library(lubridate)
library(tidyverse)
library(zoo)
library(dplyr)
library(readxl)
primary_polls <- read_excel("primary_polls.xltx", sheet = "four")
primary_polls$end_date<-mdy(primary_polls$end_date)
###rolling average
four <- primary_polls %>%
dplyr::arrange(desc(answer)) %>%
dplyr::group_by(answer) %>%
dplyr::mutate(pct_03da = zoo::rollmean(pct, k = 3, fill = NA),
pct_05da = zoo::rollmean(pct, k = 5, fill = NA),
pct_07da = zoo::rollmean(pct, k = 7, fill = NA),
pct_15da = zoo::rollmean(pct, k = 15, fill = NA),
pct_21da = zoo::rollmean(pct, k = 21, fill = NA)) %>%
dplyr::ungroup()
###rolling average chart
ggplot(four, mapping = aes(x=end_date, y=pct_07da,color=answer))+
geom_point(alpha=1/20,size=5) +
geom_smooth(method = 'loess', formula = 'y~x',span=0.5, se=FALSE, lwd=2)+
geom_hline(yintercept = 50)+
scale_colour_manual(values = c('black','gold','darkgreen',"tomato"))+
labs(title = 'Republican Presidential Primary',x='',subtitle = "Donald Trump vs The Field",
caption = "source::538.com\njamesstats.github.io/blog",
y='%Percentage%', colour=' ')+
theme_bw()+
theme(legend.position = 'bottom') +
theme(plot.title = element_text(face = "bold"),
plot.caption = element_text(face = 'italic'),axis.text = element_text(face = 'bold'),
plot.subtitle = element_text(face = "italic",size = 12))
```
## States Polling
### Iowa & New Hampshire
<p>
</P>
```{r ,iowa_newhampshire,echo = FALSE, results = 'hide', warning = FALSE,message=FALSE, fig.height=5,fig.width=10,fig.align='center', dpi=720, fig.cap="Top four candidates nation polling"}
library(ggplot2)
library(lubridate)
library(tidyverse)
library(zoo)
library(dplyr)
library(readxl)
primary_polls <- read_excel("primary_polls.xltx", sheet = "states")
primary_polls$end_date<-mdy(primary_polls$end_date)
###rolling average
states <- primary_polls %>%
dplyr::arrange(desc(answer)) %>%
dplyr::group_by(answer) %>%
dplyr::mutate(pct_03da = zoo::rollmean(pct, k = 3, fill = NA),
pct_05da = zoo::rollmean(pct, k = 5, fill = NA),
pct_07da = zoo::rollmean(pct, k = 7, fill = NA),
pct_15da = zoo::rollmean(pct, k = 15, fill = NA),
pct_21da = zoo::rollmean(pct, k = 21, fill = NA)) %>%
dplyr::ungroup()
###January primary/caucus
january<-states[states$state %in% c("Iowa","New_Hampshire"),]
###rolling average chart for january states
ggplot(january, mapping = aes(x=end_date, y=pct_07da,color=answer))+
geom_point(alpha=1/20,size=5) +
geom_smooth(method = 'loess', formula = 'y~x',span=0.5, se=FALSE, lwd=2)+
geom_hline(yintercept = 50, linetype="dashed")+
scale_colour_manual(values = c("navyblue",'black','gold','darkgreen',"tomato"))+
labs(title = 'Republican Presidential Primary/Caucus',x='',subtitle = "",
caption = "source::538.com\njamesstats.github.io/blog/Project-Republicans",
y='%Percentage%', colour=' ')+
theme_bw()+
theme(legend.position = 'bottom') +
theme(plot.title = element_text(face = "bold"),
plot.caption = element_text(face = 'italic'),axis.text = element_text(face = 'bold'),
plot.subtitle = element_text(face = "italic",size = 12),strip.text.x.top = element_text(face = "bold")) +
facet_wrap(~state)
```
### Michigan, Nevada and South Carolina
<p>
</p>
```{r ,nevada_michigan_scarolina,echo = FALSE, results = 'hide', warning = FALSE,message=FALSE, fig.height=5,fig.width=12,fig.align='center', dpi=900}
library(ggplot2)
library(lubridate)
library(tidyverse)
library(zoo)
library(dplyr)
library(readxl)
minvsc <- read_excel("~/Documents/elections_files/minvsc.xlsx")
###rolling average
states <- minvsc %>%
dplyr::arrange(desc(answer)) %>%
dplyr::group_by(answer) %>%
dplyr::mutate(pct_03da = zoo::rollmean(pct, k = 3, fill = NA),
pct_05da = zoo::rollmean(pct, k = 5, fill = NA),
pct_07da = zoo::rollmean(pct, k = 7, fill = NA),
pct_15da = zoo::rollmean(pct, k = 15, fill = NA),
pct_21da = zoo::rollmean(pct, k = 21, fill = NA)) %>%
dplyr::ungroup()
###February primary/caucus
february<-states[states$state %in% c("Nevada","Michigan","SouthCarolina"),]
###rolling average chart for February states
ggplot(february, mapping = aes(x=end_date, y=pct_03da, color=answer))+
geom_point(alpha=1/20,size=5) +
geom_smooth(method = 'loess', formula = 'y~x',span=0.7, se=FALSE, lwd=2) +
geom_hline(yintercept = 50, linetype="dashed")+
scale_colour_manual(values = c('black','gold','darkgreen',"tomato"))+
labs(title = 'Republican Presidential Primary/Caucus',x='',subtitle = "",
caption = "source::538.com\njamesstats.github.io/blog/Project-Republicans",
y='%Percentage%', colour=' ')+
theme_bw()+
theme(legend.position = 'bottom') +
theme(plot.title = element_text(face = "bold"),
plot.caption = element_text(face = 'italic'),axis.text = element_text(face = 'bold'),
plot.subtitle = element_text(face = "italic",size = 12),strip.text.x.top = element_text(face = "bold")) +
facet_wrap(~state, dir = "h")
```