-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraph-parties.r
81 lines (74 loc) · 2.54 KB
/
graph-parties.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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.65 # higher = smoother
ggplot(pollingData, aes(x = as.Date(endDate, '%Y-%m-%d'))) +
theme_bw() +
# Labour
geom_point(aes(y = LAB, colour = "LAB"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = LAB, colour = "LAB"), span = spansize, se = FALSE) +
# National
geom_point(aes(y = NAT, colour = "NAT"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = NAT, colour = "NAT"), span = spansize, se = FALSE) +
# Green
geom_point(aes(y = GRN, colour = "GRN"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = GRN, colour = "GRN"), span = spansize, se = FALSE) +
# ACT
geom_point(aes(y = ACT, colour = "ACT"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = ACT, colour = "ACT"), span = spansize, se = FALSE) +
# NZ First
geom_point(aes(y = NZF, colour = "NZF"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = NZF, colour = "NZF"), span = spansize, se = FALSE) +
# Maori
geom_point(aes(y = MRI, colour = "MRI"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = MRI, colour = "MRI"), span = spansize, se = FALSE) +
# TOP
geom_point(aes(y = TOP, colour = "TOP"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = TOP, colour = "TOP"), span = spansize, se = FALSE) +
# Y-axis
scale_y_continuous(limits = c(0, 55), breaks = c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60), 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 = "Party vote (%)", x = NULL) +
# Colors and key
scale_color_manual(
name = "",
# Legend
labels = c(
"LAB" = "Labour",
"NAT" = "National",
"ACT",
"GRN" = "Greens",
"NZF" = "NZ First",
"MRI" = "Maori",
"TOP"
),
# Color mapping
values = c(
"LAB" = "#D82A20",
"NAT" = "#004278",
"ACT" = "#FDE401",
"GRN" = "#098137",
"NZF" = "#000000",
"MRI" = "#B2001A",
"TOP" = "#32DAC3"
)
) +
theme(
legend.position = "bottom",
legend.text = element_text(size = 12)
)
# Save as 800x500 SVG