Petra Lamborn 3 years ago
parent
commit
fa35ac37a0
1 changed files with 79 additions and 6 deletions
  1. 79
    6
      votingpatterns.R

+ 79
- 6
votingpatterns.R View File

@@ -1,12 +1,35 @@
1 1
 library(dplyr)
2 2
 library(tidyr)
3
+library(ggplot2)
4
+library(scales)
5
+library(RColorBrewer)
6
+theme_set(theme_classic())
7
+partycolours <- c(
8
+                  "National Party" = "#00529F",
9
+                  "Labour Party" = "#D82A20",
10
+                  "Green Party" = "#098137",
11
+                  "New Zealand First Party" = "#000000",
12
+                  "The Opportunities Party" = "#32DAC3",
13
+                  "ACT New Zealand" = "#FDE401",
14
+                  "Conservative Party" = "#00AEEF",
15
+                  "United Future" = "#501557",
16
+                  "Māori Party" = "#B2001A"
17
+                  )
3 18
 dat <- read.csv("AdvancedVotesNewZealand.csv", stringsAsFactors = FALSE) %>%
4 19
     mutate(Party = gsub(" $", "", Party),
5
-           Party = gsub("Mâori", "Māori", Party)) %>%
6
-    pivot_wider(id_cols = c(Year, Party), names_from = Type, values_from = c(Votes, Seats)) %>%
20
+           Party = gsub("Mâori", "Māori", Party),
21
+           Party = gsub("^Conservative$", "Conservative Party", Party),
22
+           Party = gsub("^United Future New Zealand$", "United Future", Party),
23
+           Party = gsub(" Coalition$", "", Party),
24
+           Party = gsub("MANA", "Mana", Party),
25
+           Party = gsub(" \\(.*\\)$", "", Party)) %>%
26
+    pivot_wider(id_cols = c(Year, Party),
27
+                names_from = Type, values_from = c(Votes, Seats)) %>%
7 28
     mutate(Votes_Other = Votes_Total - Votes_Advance,
8
-           Advance_Proportion = Votes_Other / Votes_Total,
9
-           Seat_Difference = Seats_Advance - Seats_Total)
29
+           Advance_Proportion = Votes_Advance / Votes_Total,
30
+           Seat_Difference = Seats_Total - Seats_Advance)
31
+
32
+e.years <- sort(unique(dat$Year))
10 33
 
11 34
 elec <- dat %>% group_by(Year) %>%
12 35
     summarise(Total_Votes = sum(Votes_Total),
@@ -27,9 +50,59 @@ partyav <- partylev %>% group_by(Party) %>%
27 50
               Advance_Mean_Prop = mean(PV_Prop_Advance),
28 51
               .groups = "drop") %>% arrange(desc(Total_Mean_Prop))
29 52
 
30
-partylev %>% select(Year:Party, Votes_Advance, Votes_Other, Seats_Advance, Seats_Total, PV_Prop_Advance, PV_Prop_Total) %>%
53
+partylev$Party = factor(partylev$Party, levels = partyav$Party)
54
+
55
+typecomp <- partylev %>%
56
+    select(Year:Party, Votes_Advance,
57
+           Votes_Other, Seats_Advance,
58
+           Seats_Total, PV_Prop_Advance,
59
+           PV_Prop_Total) %>%
31 60
     pivot_longer(cols = Votes_Advance:PV_Prop_Total,
32 61
                  names_to = c("Quantity", "Type"),
33 62
                  names_pattern = "(.*)_([^_]*)$",
34 63
                  values_to = "value") %>%
35
-    pivot_wider(names_from = "Quantity", values_from = "value") %>% drop_na()
64
+    pivot_wider(names_from = "Quantity", values_from = "value")
65
+
66
+vtypes <- elec %>% select(Year, Total_Advance, Total_Other) %>%
67
+    pivot_longer(Total_Advance:Total_Other, names_to = "Type", names_prefix = "Total_",
68
+                 values_to = "Votes") %>%
69
+    mutate(Type = factor(Type, levels = c("Other", "Advance")))
70
+
71
+advplot <- ggplot(vtypes, aes(x = Year, y = Votes, fill = Type)) + geom_area() +
72
+    scale_y_continuous(labels = scales::comma, expand = c(0,0)) +
73
+    scale_x_continuous("General Election Year",
74
+                       breaks = e.years,
75
+                       expand = c(0,0)) +
76
+    scale_fill_brewer("Vote Type", palette = "Paired") +
77
+    labs(title = "Total Advance Votes", subtitle = "2002 to 2017")
78
+advplot
79
+
80
+propplot <- ggplot(filter(partylev, Party %in% names(partycolours)),
81
+       aes(x = Year, y = Advance_Proportion, colour = Party)) + geom_line() +
82
+    scale_colour_manual(values = partycolours) +
83
+    scale_y_continuous("Percent of votes from Advance votes",
84
+                       labels = scales::percent, limits=c(0, NA), expand=c(0,0)) +
85
+    scale_x_continuous("General Election Year",
86
+                       breaks = e.years,
87
+                       expand = c(0,0)) +
88
+    labs(title = "Share of Each Party's Votes From Advanced Votes by Year")
89
+propplot
90
+
91
+
92
+ggplot(filter(partylev, Party %in% names(partycolours)),
93
+       aes(x = Year, y = Seat_Difference, colour = Party)) +
94
+    geom_line() +
95
+    scale_colour_manual(values = partycolours) +
96
+    scale_x_continuous("General Election Year",
97
+                       breaks = e.years, expand = c(0,0)) +
98
+    labs(title = )
99
+
100
+svp <- partylev %>% mutate(PV_Prop_Difference = PV_Prop_Total - PV_Prop_Advance) %>%
101
+    select(Year, Party, Seat_Difference, PV_Prop_Difference) %>%
102
+    filter(Party %in% names(partycolours))
103
+
104
+ggplot(svp, aes(PV_Prop_Difference, Seat_Difference, colour = Party)) + geom_point() +
105
+    scale_colour_manual(values = partycolours) +
106
+    scale_x_continuous("Party Vote Proportion Difference", labels = scales::percent) +
107
+    labs(title = "Relationship between vote difference and seat difference",
108
+         y = "Seat Difference")