Created
July 18, 2016 06:44
-
-
Save HughParsonage/973b1d08f2e78c98f6f85d63da90814f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
library(data.table) | |
library(dplyr) | |
library(dtplyr) | |
library(broom) | |
library(grid) | |
library(gridExtra) | |
library(gtable) | |
library(ggplot2) | |
library(ggthemes) | |
library(viridis) | |
library(grattan) | |
library(scales) | |
library(eechidna) # 2013 boundaries | |
`%notin%` <- function(x, y) !x %in% y | |
year = "2016" | |
switch(year, | |
"2016" = { | |
senate <- fread("http://vtr.aec.gov.au/Downloads/SenateFirstPrefsByDivisionByVoteTypeDownload-20499.csv") | |
}, | |
"2013" = { | |
senate <- fread("http://results.aec.gov.au/17496/website/Downloads/SenateFirstPrefsByDivisionByVoteTypeDownload-17496.csv") | |
}) | |
votes_by_majorness <- | |
senate[, | |
Other := !grepl(paste0("(Labor)|(Liberal National)|(National Party)|", | |
"(Greens)|", | |
"(Country Liberals)|(Liberal.The National)|", | |
"(Liberal\\s.\\sNationals)|(^(The Nationals)$)|(^(Liberal)$)"), | |
PartyName) | |
] %>% | |
group_by(DivisionNm, Other) %>% | |
summarise(n_votes = sum(TotalVotes)) %>% | |
ungroup %>% | |
group_by(DivisionNm) %>% | |
mutate(prop_votes = n_votes / sum(n_votes)) %>% | |
mutate(ELECT_DIV2 = gsub("[^A-Z]", "", toupper(DivisionNm))) %>% | |
filter(Other) | |
switch(year, | |
"2016" = { | |
seat_coding <- | |
fread("hexagons/2016SeatGeoCoding.csv") %>% | |
rename(id = SeatCd) %>% | |
mutate(ELECT_DIV2 = toupper(gsub("[^A-Z]", "", Seat))) %>% | |
mutate(ELECT_DIV2 = ifelse(ELECT_DIV2 == "THROSBY", | |
"WHITLAM", | |
ifelse(ELECT_DIV2 == "FRASER", | |
"FENNER", | |
ELECT_DIV2))) | |
"2013" = { | |
seat_coding <- | |
fread("hexagons/SeatGeoCoding.csv") %>% | |
rename(id = SeatCd) %>% | |
mutate(ELECT_DIV2 = toupper(gsub("[^A-Z]", "", Seat))) | |
} | |
}) | |
switch( | |
year, | |
"2016" = { | |
elect_2016 <- readRDS("elect_2016_simple.rds") | |
elect_2016@data$id <- seq_along(nrow(elect_2016@data)) | |
Electoral_divisions_by_id <- readRDS("elect_div_by_id.rds") | |
elect_2016_swept <- tidy(elect_2016) | |
elect_2016_swept <- merge(elect_2016_swept, Electoral_divisions_by_id, by = "id") %>% | |
as.data.table | |
}, | |
"2013" = { | |
elect_2016_swept <- | |
nat_map %>% # eechidna:: | |
mutate(ELECT_DIV2 = gsub("[^A-Z]", "", toupper(ELECT_DIV))) %>% | |
as.data.table | |
} | |
) | |
joined <- | |
elect_2016_swept %>% | |
merge(merge(seat_coding, votes_by_majorness, by = "ELECT_DIV2"), by = "ELECT_DIV2") %>% | |
# limit to 40% | |
mutate(prop_votes_factor = factor(percent(pmin(floor(prop_votes * 20) / 20, 0.4)), | |
levels = percent(seq(0.1, 0.4, by = 0.05)))) %>% | |
as.data.table | |
metros <- | |
joined %>% | |
filter(`Metro/Regional` == "Metro") %>% | |
melt.data.table(measure.vars = c("Sydney", "Melbourne", "Brisbane", "Adelaide", "Perth"), | |
variable.name = "City", | |
value.name = "is_in") %>% | |
filter(as.logical(is_in)) | |
make_plot <- function(.data, guide = FALSE, the_city = NULL){ | |
if (guide) guide <- "legend" | |
if (!is.null(the_city)){ | |
arrange(.data, group, order) %>% | |
{ | |
x_range <- max(.$long) - min(.$long) | |
y_range <- max(.$lat) - min(.$lat) | |
ggplot(., aes(x = long, y = lat, group = group, order = order)) + | |
geom_polygon(aes(fill = prop_votes_factor), color = "black") + | |
theme_map(base_size = 20) + | |
# scale_fill_viridis("Proportion votes to non-major parties\nexceeded:", | |
# discrete = TRUE, drop = FALSE, guide = guide) + | |
scale_fill_discrete_grattan("Proportion votes to non-major parties\nexceeded:", | |
drop = FALSE, guide = guide) + | |
coord_map() + | |
theme(legend.position = c(0, 1), | |
legend.justification = c(0, 1), | |
legend.background = element_rect(fill = NA)) + | |
# annotate("text", | |
# x = min(.$long) + x_range * 1.05, | |
# y = min(.$lat) + y_range * 0.5, | |
# hjust = 0.75, | |
# color = "black", | |
# fontface = "bold", | |
# size = 14/(14/5), | |
# label = the_city) | |
ggtitle(the_city) + | |
theme(plot.title = element_text(size = rel(1), margin = margin(0,0,0,0), vjust = 1)) | |
} | |
} else { | |
arrange(.data, group, order) %>% | |
ggplot(aes(x = long, y = lat, group = group, order = order)) + | |
geom_polygon(aes(fill = prop_votes_factor), color = "black", size = 0.1) + | |
theme_map(base_size = 20) + | |
# scale_fill_viridis("Proportion votes to non-major parties\nexceeded:", | |
# discrete = TRUE, drop = FALSE, guide = guide) + | |
scale_fill_discrete_grattan("Senate 1st pref. votes to\nnon-major parties\nexceeded:", | |
drop = FALSE, guide = guide) + | |
coord_map() + | |
theme(legend.position = c(0, 1), | |
legend.justification = c(0, 1), | |
legend.background = element_rect(fill = NA)) | |
} | |
} | |
plot_city <- function(city){ | |
if (city == "Sydney"){ | |
metros %>% | |
filter(City == "Sydney") %>% | |
# Avoid Lord Howe Island | |
filter(between(lat, -35, -33)) %>% | |
make_plot(the_city = city) | |
} else { | |
metros %>% | |
filter(City == city) %>% | |
make_plot(the_city = city) | |
} | |
} | |
joined %>% | |
filter(`Metro/Regional` != "Metro") %>% | |
filter(long > 113) %>% | |
make_plot(guide = TRUE) | |
vplayout <- function(x, y) viewport(layout.pos.col = x, layout.pos.row = y) | |
grid.newpage() | |
# Prepare the plotting area. 9 x 8 cells. | |
pushViewport(viewport(layout = grid.layout(8, 9))) # reversed | |
print({ | |
# Australia | |
joined %>% | |
filter(long > 113) %>% | |
mutate(prop_votes_factor = factor(ifelse(`Metro/Regional` == "Metro", | |
NA_character_, | |
as.character(prop_votes_factor)), | |
levels = percent(seq(0.1, 0.4, by = 0.05)))) %>% | |
make_plot(guide = TRUE) | |
}, | |
# push this plot to cover the cells with x-coordinates 1-9 and y-coordinates 1-6. | |
vp = vplayout(1:9, 1:6)) | |
print(plot_city("Perth"), vp = vplayout(1:2, 3:4 + 1)) | |
print(plot_city("Adelaide"), vp = vplayout(4:5, 5:6)) | |
print(plot_city("Melbourne"), vp = vplayout(6:8, 5:7 + 1)) | |
print(plot_city("Sydney"), vp = vplayout(7:9, 3:5 + 1)) | |
print(plot_city("Brisbane"), vp = vplayout(7:9, 1:3)) | |
# Copy to a pdf 22 inches by 14 inches. | |
dev.copy2pdf(file = paste0(year, "-Protest-senate-votes-by-Division.pdf"), width = 22, height = 14) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment