Skip to content

Instantly share code, notes, and snippets.

@walkerke
Last active February 2, 2018 14:34
Show Gist options
  • Save walkerke/2d534dc0dd638ccdbaeef1ca83f4fe86 to your computer and use it in GitHub Desktop.
Save walkerke/2d534dc0dd638ccdbaeef1ca83f4fe86 to your computer and use it in GitHub Desktop.
library(tidycensus)
library(leaflet)
library(sf)
library(viridis)
options(tigris_use_cache = TRUE)
il1 <- get_acs(geography = "county",
variables = c(hhincome = "B19013_001"),
state = "IL",
geometry = TRUE) %>%
st_transform(4326)
il2 <- get_acs(geography = "tract",
variables = c(hhincome = "B19013_001"),
state = "IL",
geometry = TRUE) %>%
st_transform(4326)
bins <- c(0, 30000, 40000, 50000, 60000, 70000, 80000, 250000)
pala <- colorBin("viridis", il1$estimate, bins = bins)
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(data = il1, stroke = FALSE, smoothFactor = 0.2,
color = ~pala(estimate),
label = ~as.character(estimate),
fillOpacity = 0.8,
group = "Counties") %>%
addPolygons(data = il2, stroke = FALSE, smoothFactor = 0.2,
color = ~pala(estimate),
label = ~as.character(estimate),
fillOpacity = 0.8,
group = "Tracts") %>%
addLegend(pal = pala, values = il1$estimate,
title = "Med. HH Income") %>%
addLayersControl(overlayGroups = c("Tracts", "Counties")) %>%
hideGroup("Tracts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment