Skip to content

Instantly share code, notes, and snippets.

@tiernanmartin
Last active May 31, 2018 15:39
Show Gist options
  • Save tiernanmartin/eaa589e6a51df0144d76aac90b4c6cda to your computer and use it in GitHub Desktop.
Save tiernanmartin/eaa589e6a51df0144d76aac90b4c6cda to your computer and use it in GitHub Desktop.
Map public libraries in "distressed" counties (WA)
# SETUP ----
library(mapview)
library(tigris)
library(rvest)
library(sf)
library(tidyverse)
options(tigris_class = "sf")
# GET DATA ----
libs <- read_sf("https://github.com/tiernanmartin/datasets/raw/master/wa-libraries/data/wa-libraries.gpkg")
counties <- tigris::counties(state = "WA", cb = TRUE) %>%
st_transform(4326)
distressed_list_url <- "https://fortress.wa.gov/esd/employmentdata/reports-publications/regional-reports/distressed-areas-list"
distr_table <- read_html(distressed_list_url) %>%
html_nodes("table") %>%
html_table(header = TRUE) %>%
flatten_df() %>%
set_names(c("NAME","DISTR_VAR","DISTR_LGL")) %>%
transmute(COUNTY_NAME = str_replace(NAME," County",""),
DISTR_VAR,
DISTR_LGL = DISTR_LGL %in% "Yes")
# JOIN DATA ---
distressed_sf <- left_join(counties, distr_table, by = c(NAME = "COUNTY_NAME")) %>%
rename(COUNTY_NAME = NAME) %>%
st_sf
distr_libs <- st_join(libs, distressed_sf) %>%
st_cast("POINT")
# SUMMARY TABLE ----
distr_libs %>%
st_set_geometry(NULL) %>%
count(DISTR_LGL, COUNTY_NAME) %>%
arrange(DISTR_LGL, desc(n)) %>%
print(n=Inf)
# MAP -----
distr_libs %>%
transmute(LIBRARY_NAME,
COUNTY_NAME,
"IN A DISTRESSED COUNTY" = factor(DISTR_LGL)) %>%
mapview(zcol = "IN A DISTRESSED COUNTY", legend = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment