l— output: html_document: default —

library(tidycensus)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
library(viridis)
## Loading required package: viridisLite
library(stringr)
census_api_key("3ba2cd13dc1173b587e8143741a93067e61da071")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
library(leaflet)
library(ggmap)
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
devtools::install_github("cwickham/geospatial")
## Skipping install of 'geospatial' from a github remote, the SHA1 (c5df470b) has not changed since last install.
##   Use `force = TRUE` to force installation
library(geospatial)
v15 <- load_variables(2015, "acs5", cache = TRUE)
options(tigris_use_cache = TRUE)
medhh <- get_acs(state = "WA", geography = "county", 
                  variables = "B19013_001", geometry = TRUE)
Pierce <- get_acs(state = "WA", county = "Pierce", geography = "tract", variables = "B19013_001", geometry = TRUE)
head(Pierce)
## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -122.489 ymin: 47.09672 xmax: -122.2586 ymax: 47.27054
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 x 6
##         GEOID                                           NAME   variable
##         <chr>                                          <chr>      <chr>
## 1 53053060700    Census Tract 607, Pierce County, Washington B19013_001
## 2 53053061500    Census Tract 615, Pierce County, Washington B19013_001
## 3 53053063100    Census Tract 631, Pierce County, Washington B19013_001
## 4 53053071208 Census Tract 712.08, Pierce County, Washington B19013_001
## 5 53053071403 Census Tract 714.03, Pierce County, Washington B19013_001
## 6 53053071504 Census Tract 715.04, Pierce County, Washington B19013_001
## # ... with 3 more variables: estimate <dbl>, moe <dbl>,
## #   geometry <simple_feature>
pal <- colorQuantile(palette = "viridis", domain = Pierce$estimate, n = 10)
Pierce %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "Thunderforest.TransportDark") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ pal(estimate)) %>%
    addLegend("bottomright", 
              pal = pal, 
              values = ~ estimate,
              title = "Income percentiles",
              opacity = 2)