Skip to content

Instantly share code, notes, and snippets.

@emitanaka
Last active April 30, 2020 01:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emitanaka/2675d0b019dc092b0f8ea2ad6424b886 to your computer and use it in GitHub Desktop.
Save emitanaka/2675d0b019dc092b0f8ea2ad6424b886 to your computer and use it in GitHub Desktop.
Instant-runoff voting system for the ๐Ÿ‡ฆ๐Ÿ‡บ House of Representative election in 2019
# the party logos are required to be in the form of `plogo-XXX.png` where XXX is the party abbreviation that matches
# in PartyAb
# inspired by https://coolbutuseless.github.io/2020/04/12/racing-barplots-with-ggpattern-flagon-gganimate/
library(tidyverse)
library(gganimate)
library(ggpattern)
dat <- read_csv("https://results.aec.gov.au/24310/Website/Downloads/HouseDopByDivisionDownload-24310.csv", skip = 1) %>%
mutate(PartyAb = case_when(
PartyAb == "GVIC" ~ "GRN",
PartyAb == "LP" ~ "LNP",
PartyAb == "NP" ~ "LNP",
TRUE ~ PartyAb
))
adivision <- "Monash"
# which strangely doesn not include Monash Clayton Campus!
dat %>%
filter(CalculationType == "Preference Count" & DivisionNm == adivision) %>%
filter(CalculationValue!=0) %>%
# too lazy to collect all logos so only for GRN, LNP and ALP
mutate(logo = case_when(
PartyAb %in% c("GRN", "LNP", "ALP") ~ paste0("plogo-", PartyAb, ".png"),
TRUE ~ "")) %>%
group_by(CountNumber) %>%
arrange(-CalculationValue) %>%
mutate(rank = 1:n()) %>%
ggplot(aes(group = CandidateID)) +
geom_rect_pattern(
aes(
xmin = 0,
xmax = CalculationValue / 1000,
ymin = rank - 0.45,
ymax = rank + 0.45,
pattern_filename = I(logo)
),
pattern = 'image',
fill = 'grey70',
pattern_gravity = 'East', # Push flag to the right
pattern_type = 'none', # no auto-rescale
pattern_scale = -2 # Scale to height.
) +
geom_text(aes(y = rank, label = Surname), x = -4, col = "gray13", hjust = "right", size = 7) +
geom_text(aes(label = paste("Count: ", CountNumber + 1)), x = 60 , y = -7, size = 14, col = "grey18") +
scale_y_reverse() +
labs(x = "Votes in '000s") +
theme_bw() +
theme(
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
legend.key.size = unit(2, 'cm'),
axis.text = element_text(size = 20),
axis.title = element_text(size = 20)
) +
scale_x_continuous(
limits = c(-40, 80),
breaks = c(0, 20, 40, 60, 80)
) +
transition_states(CountNumber,
transition_length = 2,
state_length = 1) +
ease_aes('cubic-in-out') +
exit_fade()
@emitanaka
Copy link
Author

emitanaka commented Apr 13, 2020

Output:

output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment