Last active
October 20, 2019 08:04
-
-
Save thoughtfulbloke/279f9e33b12c3c3d351609597b2eb027 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(rvest) | |
library(dplyr) | |
library(tidyr) | |
library(ggplot2) | |
library(stringr) | |
library(ggbeeswarm) | |
html = read_html("https://www.electionresults.govt.nz/electionresults_2017/electorate-status.html") | |
lnks <- (html %>% html_nodes("a") %>% html_attr("href"))[25:95] | |
urls <- tibble(lnks) %>% separate(lnks, into=c("before","url"), sep=2) | |
x <- urls$url[1] | |
candy <- function(x){ | |
electlink <- paste0("https://www.electionresults.govt.nz/electionresults_2017/", x) | |
election <- read_html(electlink) | |
eresult <- html_table(html_nodes(election, "table"), fill=TRUE) | |
candidates <- eresult[[3]] | |
candidates$electorate <- x | |
return(candidates) | |
} | |
eresult <- bind_rows(lapply(urls$url, candy)) | |
eresult$votes <- as.numeric(gsub("[^1234567890]","",eresult$Candidates)) | |
eresult$enum <- as.numeric(gsub("[^1234567890]","",eresult$electorate)) | |
eresult$letter1 <- sapply(eresult$Candidates, | |
function(x){unlist(strsplit(x, | |
split = ""))[1]}) | |
eresult %>% filter(!is.na(votes), enum < 65, | |
!str_detect(Candidates, fixed("Candidate ")), | |
!str_detect(Candidates, fixed("TOTAL:"))) %>% | |
group_by(electorate) %>% | |
mutate(percent = 100 * votes/sum(votes)) %>% ungroup() %>% | |
mutate(letter1 = factor(letter1, levels=LETTERS)) %>% | |
ggplot(aes(x=as.numeric(letter1), y=percent)) + | |
geom_quasirandom(alpha=0.3) + geom_smooth() + | |
ggtitle("General electorate candidate vote share 2017 NZ general election, | |
by first letter of surname") + xlab("First letter") + | |
scale_x_continuous(breaks=1:26, labels=LETTERS) + | |
theme_minimal() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment