-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraph-blocs.r
59 lines (52 loc) · 1.75 KB
/
graph-blocs.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
# In RStudio, Ctrl+A then Run
library(ggplot2)
library(dplyr)
setwd("~/GitHub/nzpolls/graphing") # replace with own working directory
# Read data
csvData <- read.csv("local-data.csv")
pollingData <- arrange(csvData, desc(as.Date(endDate, '%Y-%m-%d')))
# Plot data
spansize <- 0.5 # higher = smoother
ggplot(pollingData, aes(x = as.Date(endDate, '%Y-%m-%d'))) +
theme_bw() +
# Left
geom_point(aes(y = leftBloc, colour = "Left"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = leftBloc, colour = "Left"), span = spansize, se = FALSE) +
# Right
geom_point(aes(y = rightBloc, colour = "Right"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = rightBloc, colour = "Right"), span = spansize, se = FALSE) +
# Other
geom_point(aes(y = otherBloc, colour = "Other"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = otherBloc, colour = "Other"), span = spansize, se = FALSE) +
# Y-axis
scale_y_continuous(limits = c(30, 65), breaks = c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70), minor_breaks = waiver(), expand = c(0, 0)) +
# X-axis
scale_x_date(date_breaks = "4 months", date_labels = "%b '%y", minor_breaks = "1 month") +
# Axis styling
theme(
plot.background = element_rect(fill = "white", color = NA),
axis.text.x = element_text(angle = 0, vjust = 0.5, size = 10),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 12)
) +
# Axis labels
labs(y = "Combined party vote (%)", x = NULL) +
# Colors and key
scale_color_manual(
name = "",
# Legend
labels = c(
"Left" = "LAB+GRN+MRI",
"Right" = "NAT+ACT+NZF"
),
# Color mapping
values = c(
"Left" = "#B85300",
"Right" = "#5000B8"
)
) +
theme(
legend.position = "bottom",
legend.text = element_text(size = 12)
)
# Save as 800x500 SVG