Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created November 8, 2020 00:32
Show Gist options
  • Save thoughtfulbloke/b5132bb4ffdabd09778b82bc71155d6a to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/b5132bb4ffdabd09778b82bc71155d6a to your computer and use it in GitHub Desktop.
library(dplyr)
library(ggplot2)
library(ggthemes)
library(maps)
library(mapdata)
combined <- read.csv("combined.csv", stringsAsFactors = FALSE)
top_support <- max(combined$vote)
# keep in mind one polling place can provide for multiple electorates
top_support <- combined %>%
group_by(lat, lon, p_abbrev) %>%
summarise(support= sum(vote)) %>%
group_by(lat, lon) %>%
mutate(percent = 100 * support/ sum(support)) %>%
group_by(p_abbrev) %>%
summarise(mx = max(percent, na.rm=TRUE))
graph_data <- combined %>%
group_by(lat, lon, p_abbrev) %>%
summarise(support= sum(vote)) %>%
group_by(lat, lon) %>%
mutate(percent = 100 * support/ sum(support)) %>%
ungroup() %>%
filter(!is.nan(percent)) %>%
mutate(percent_range= factor(case_when(percent < 5 ~ ">=00, <05",
percent < 10 ~ ">=05, <10",
percent < 15 ~ ">=10, <15",
percent < 20 ~ ">=15, <20",
percent < 25 ~ ">=20, <25",
percent < 30 ~ ">=25, <30",
percent < 35 ~ ">=30, <35",
percent < 40 ~ ">=35, <40",
percent < 45 ~ ">=40, <45",
percent < 50 ~ ">=45, <50"))) %>%
ungroup() %>%
filter(p_abbrev == "ACT")
#nz <- map_data("nz")
nz <- map_data("nzHires")
nzmap <- ggplot(nz, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", colour = "#666666", size=0.1)
nzmap
themecol <- "#f9c013"
finished_map <- nzmap + coord_map(xlim=c(166,178), ylim=c(-47,-34)) +
geom_point(data=graph_data, aes(x=lon,y=lat, group=NULL),
size=0.1, col="#f9c013") + theme_tufte() + facet_wrap(~percent_range, ncol=5) +
ggtitle("New Zealand 2020 General Election, individual polling sites (excluding Chatham Islands)
provisional percentage voted ACT Party at polling place. ") +
theme(plot.title = element_text(colour = themecol[1], margin=margin(b = 12, unit = "pt"),
family = "Open Sans Bold", lineheight=1.16),
panel.grid = element_blank(),
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.key.size = unit(2, 'lines'),
strip.background = element_rect(fill= "#FFFFFF", colour="#EFEFEF"),
strip.placement = "inside",
legend.position = "none",
strip.text = element_text(size=13, margin=margin(t=3, b=3, unit = "pt"),
family = "Open Sans Regular"),
panel.background = element_rect(fill = "#FFFFFF", colour = "#FFFFFF"),
panel.spacing = unit(1.5, "lines"),
plot.background = element_rect(fill = "#FAFAFA", colour = NA))
finished_map
ggsave(filename = "~/Desktop/yellow.png", plot=finished_map, width = 42, height=29.7,
units="cm", dpi = 150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment