-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
62 lines (53 loc) · 2.78 KB
/
Copy pathREADME.Rmd
File metadata and controls
62 lines (53 loc) · 2.78 KB
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
---
title: "ECQ Primary Vote data"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message=FALSE)
```
If you've run the ECQ scraper script you should have a file called `primary_votes.csv` in your `Data` folder. The scraper script pulls down the results pages from the [ECQ website](https://results.ecq.qld.gov.au/elections/state/State2017/results/summary.html) and extracts the results summary table for each electorate. I realise that there's a [downloadable zip file with an XML file](https://results.ecq.qld.gov.au/elections/state/State2017/mediainformation.html) but trying to deal with unzipping an XML and parsing the tree seemed to be too difficult. In any case, the HTML data is is all coerced into a tidy data frame with each candidate's party affiliation (`IND` if none given), raw number of formal, primary votes, and the percentage of the vote for that electorate received. A full distribution of preferences is not available yet.
``` {r}
# source("ecq_scraper.R")
library(tidyverse)
primary_votes <- read_csv("Data/primary_votes.csv")
head(primary_votes)
```
We need to choose some colours for each party running. These are taken from the [Wikipedia template](https://en.wikipedia.org/wiki/Category:Australia_political_party_colour_templates) except for `CR`, the Consumer Rights & No-Tolls party.
``` {r}
cols <- unlist(list(ALP = "#DE3533",
GRN = "#39b54a",
LNP = "#1456F1",
IND = "#808080",
KAP = "#b50204",
ONP = "#F8F16F",
CR = "purple"))
```
We also need to process the data to show who is independent, and to order the candidates from largest to smallest vote (with 1 being the largest vote)
``` {r}
to_plot <- mutate(primary_votes,
Party = if_else(is.na(Party),
"IND",
Party)) %>%
group_by(Electorate) %>%
arrange(desc(Percent)) %>%
mutate(order = 1:n()) %>%
arrange(Electorate, order)
```
Now consider all electorates shown as pie charts, with candidates ordered by share of vote
``` {r, fig.width=7, fig.height=21, dpi=300}
ggplot(data=to_plot,
aes(x=0, y=Percent)) +
geom_col(aes(fill=Party, group=order),
position=position_stack(),
color="black") +
coord_polar(theta = "y") +
facet_wrap( ~ Electorate, nrow=16) +
theme_minimal() +
theme(legend.position="top", axis.text = element_blank(),
strip.text = element_text(size=7),
panel.grid = element_blank()) +
scale_fill_manual(values = cols) +
xlab("") + ylab("") +
ggtitle(label = "Primary vote for each candidate in each electorate",
subtitle = "2017 Queensland State Election, preliminary ECQ results")
```