library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.3.3
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'purrr' was built under R version 3.3.3
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(viridis)
## Warning: package 'viridis' was built under R version 3.3.3
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 3.3.3
library(stringr)
## Warning: package 'stringr' was built under R version 3.3.3
census_api_key("7491cf955005ef7221778ebe1228115c53a12df6")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
v15 <- load_variables(2015, "acs5", cache = TRUE)
options(tigris_use_cache = TRUE)
medhh <- get_acs(state = "WA", geography = "county",
variables = "B19013_001", geometry = TRUE)
head(medhh)
## Simple feature collection with 6 features and 5 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -124.7631 ymin: 45.83596 xmax: -117.8194 ymax: 48.55072
## epsg (SRID): 4269
## proj4string: +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 × 6
## GEOID NAME variable estimate moe
## <chr> <chr> <chr> <dbl> <dbl>
## 1 53005 Benton County, Washington B19013_001 60251 1370
## 2 53007 Chelan County, Washington B19013_001 51837 1886
## 3 53009 Clallam County, Washington B19013_001 47253 1606
## 4 53015 Cowlitz County, Washington B19013_001 47452 1861
## 5 53037 Kittitas County, Washington B19013_001 46458 3093
## 6 53043 Lincoln County, Washington B19013_001 46069 1791
## # ... with 1 more variables: geometry <S3: sfc_MULTIPOLYGON>
kinginc <- get_acs(state = "WA", county = "King", geography = "tract", variables = "B19013_001", geometry = TRUE)
head(kinginc)
## Simple feature collection with 6 features and 5 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -122.3556 ymin: 47.638 xmax: -122.2903 ymax: 47.73405
## epsg (SRID): 4269
## proj4string: +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 × 6
## GEOID NAME variable
## <chr> <chr> <chr>
## 1 53033000200 Census Tract 2, King County, Washington B19013_001
## 2 53033002800 Census Tract 28, King County, Washington B19013_001
## 3 53033003800 Census Tract 38, King County, Washington B19013_001
## 4 53033004301 Census Tract 43.01, King County, Washington B19013_001
## 5 53033004900 Census Tract 49, King County, Washington B19013_001
## 6 53033006100 Census Tract 61, King County, Washington B19013_001
## # ... with 3 more variables: estimate <dbl>, moe <dbl>, geometry <S3:
## # sfc_MULTIPOLYGON>
kinginc %>%
mutate(NAME = str_replace(NAME,'King County, Washington','')) %>%
mutate(NAME = str_replace(NAME, "Census Tract","")) %>%
ggplot(aes(x = estimate, y = reorder(NAME, estimate))) +
geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

#kinginc %>%
# ggplot(aes(fill = estimate, color = estimate)) +
# geom_sf() +
# coord_sf(crs = 26911) +
# scale_fill_viridis(option = "magma") +
# scale_color_viridis(option = "magma")
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.3.3
library(stringr)
library(sf)
## Warning: package 'sf' was built under R version 3.3.3
## Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
library(dplyr)
pal <- colorQuantile(palette = "viridis", domain = kinginc$estimate, n = 10)
kinginc %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "Esri.WorldStreetMap") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.4,
color = ~ pal(estimate)) %>%
addLegend("bottomright",
pal = pal,
values = ~ estimate,
title = "Income percentiles",
opacity = 1)