Skip to content

Instantly share code, notes, and snippets.

@johnburnmurdoch
Created February 13, 2020 17:35
Show Gist options
  • Save johnburnmurdoch/10647d5ec4a483c887346fe0251d0696 to your computer and use it in GitHub Desktop.
Save johnburnmurdoch/10647d5ec4a483c887346fe0251d0696 to your computer and use it in GitHub Desktop.
install.packages("needs")
library(needs)
needs(tidyverse, magrittr, rvest, sf, raster, rgdal)
ireland_constits <- c("https://en.wikipedia.org/wiki/Carlow%E2%80%93Kilkenny_(D%C3%A1il_constituency)") %>%
read_html() %>%
html_nodes('div[aria-labelledby="Current_Dáil_constituencies"] tr:nth-child(2) ul li a') %>%
html_attr("href") %>%
tail(-1) %>%
c("/wiki/Carlow%E2%80%93Kilkenny_(D%C3%A1il_constituency)", .)
ireland_constit_results <- paste0("https://en.wikipedia.org", ireland_constits) %>%
imap_dfr(~{
read_html(.x) %>%
html_nodes("table.wikitable") %>%
map_dfr(~{
council <- .x %>% html_node("th") %>% html_text() %>% str_replace_all("(2020 general election\\: )|\\[.+","")
title <- .x %>% html_node("th a") %>% html_attr("title") %>% replace_na("foo")
content <- .x
if(title == "2020 Irish general election"){
party <- content %>% html_nodes("td:nth-child(2)") %>% html_text(T)
FPv <- content %>% html_nodes("td:nth-child(4)") %>% html_text(T) %>% parse_number()
return(tibble(party, FPv) %>% mutate(council))
}else{
}
}) %>%
compact()
})
unzip("~/Downloads/ne_10m_populated_places.zip", exdir = "~/Downloads/ireland_pop_dens/populated_places")
populated_places <- st_read("~/Downloads/ireland_pop_dens/populated_places/ne_10m_populated_places.shp")
populated_places_IRL <- populated_places %>%
filter(ADM0NAME == "Ireland") %>%
dplyr::select(NAME, NATSCALE, geometry, LONGITUDE, LATITUDE)
unzip("~/Downloads/ne_10m_admin_0_map_subunits.zip", exdir = "~/Downloads/GIS/world")
ireland_island_map <- st_read("~/Downloads/GIS/world/ne_10m_admin_0_map_subunits.shp") %>% st_transform(4326) %>% filter(GU_A3 %in% c("IRL", "NIR"))
# Ireland constituency shapefiles source: https://data.gov.ie/dataset/constituency-boundaries-ungeneralised-2017-osi-national-electoral-boundaries
unzip("~/Downloads/Constituency_Boundaries_Ungeneralised_2017__OSi_National_Electoral_Boundaries.zip", exdir = "~/Downloads/ireland_pop_dens/constituencies_2017")
ireland_constits_map <- st_read("~/Downloads/ireland_pop_dens/constituencies_2017/Constituency_Boundaries_Ungeneralised_2017__OSi_National_Electoral_Boundaries.shp") %>% st_transform(4326)
ireland_constits_map %>%
dplyr::select(ENGLISH_NA, geometry) %>%
mutate(council = as.character(ENGLISH_NA) %>% str_replace_all("\\s\\(.+", "") %>% str_replace_all("^D$", "Dún Laoghaire")) %>%
left_join(
ireland_constit_results %>%
filter(party == "Sinn Féin") %>%
group_by(party, council) %>%
summarise(FPv = sum(FPv)) %>%
ungroup() %>%
mutate(council = council %>% str_replace_all("–","-"))
) %>%
mutate(FPv = replace(FPv, is.na(FPv), 0)) -> ireland_constits_map_SF
ggplot() +
theme_minimal() +
theme(
panel.grid.major = element_line(colour = 'transparent'),
axis.text = element_blank(),
axis.title = element_blank()
) +
geom_sf(data=ireland_island_map, fill = "#CCCCCC", col = "transparent", size=0) +
geom_sf(data=ireland_constits_map_SF, aes(fill = FPv), col = "transparent", size=0) +
geom_sf(data=populated_places_IRL %>% filter(NATSCALE >= 0), col = "black") +
geom_text(data=populated_places_IRL %>% filter(NATSCALE >= 0), aes(label = NAME, x = LONGITUDE, y = LATITUDE), col = "black", hjust=0) +
scale_fill_gradientn(colours=c("#f2f2ee","#8d1a14"), limits=c(0,50), guide = guide_colorbar(ttile = "First preference votes (%)"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment