Using tidycensus and leaflet to map Median Age

library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0     ✔ purrr   0.2.5
## ✔ tibble  1.4.2     ✔ dplyr   0.7.8
## ✔ tidyr   0.8.2     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidycensus)

library(leaflet)
library(stringr)
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.3, PROJ 4.9.2
#To prevent display of data loading in knitted document... 
options(tigris_use_cache = TRUE)

See Kyle Walker’s article about the basic usage of his tidycensus package. The article includes instructions for obtaining and installing the census_api_key. It also includes information about finding parameter values to use in the get_acs() and get_decennial() functions.

# https://walkerke.github.io/tidycensus/articles/basic-usage.html

# census_api_key(overwrite = TRUE,"f4caf892e49de4ac853951c6ddfb4c8a2572c88e", install = TRUE)
# readRenviron("~/.Renviron")

# List of values for the 'variables = ' parameters
#   v15 <- load_variables(2016, "acs5", cache = TRUE)
#   View(v15)

wa_age <- get_acs(geography = "county", 
                     variables = "B01002_001", 
                     state = "WA",
                     geometry = TRUE) 
## Getting data from the 2012-2016 5-year ACS
wa_age
## Simple feature collection with 39 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7631 ymin: 45.54354 xmax: -116.916 ymax: 49.00249
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
## First 10 features:
##    GEOID                        NAME   variable estimate moe
## 1  53001    Adams County, Washington B01002_001     28.6 0.3
## 2  53003   Asotin County, Washington B01002_001     44.6 0.2
## 3  53005   Benton County, Washington B01002_001     35.5 0.2
## 4  53007   Chelan County, Washington B01002_001     39.8 0.8
## 5  53009  Clallam County, Washington B01002_001     50.3 0.3
## 6  53011    Clark County, Washington B01002_001     37.8 0.2
## 7  53013 Columbia County, Washington B01002_001     49.9 1.1
## 8  53015  Cowlitz County, Washington B01002_001     41.7 0.4
## 9  53017  Douglas County, Washington B01002_001     37.1 0.7
## 10 53019    Ferry County, Washington B01002_001     48.2 0.7
##                          geometry
## 1  MULTIPOLYGON (((-119.3694 4...
## 2  MULTIPOLYGON (((-117.48 46....
## 3  MULTIPOLYGON (((-119.8751 4...
## 4  MULTIPOLYGON (((-121.1789 4...
## 5  MULTIPOLYGON (((-124.6022 4...
## 6  MULTIPOLYGON (((-122.796 45...
## 7  MULTIPOLYGON (((-118.2415 4...
## 8  MULTIPOLYGON (((-123.2183 4...
## 9  MULTIPOLYGON (((-120.3178 4...
## 10 MULTIPOLYGON (((-118.8696 4...
summary(wa_age)
##     GEOID               NAME             variable            estimate    
##  Length:39          Length:39          Length:39          Min.   :24.20  
##  Class :character   Class :character   Class :character   1st Qu.:37.15  
##  Mode  :character   Mode  :character   Mode  :character   Median :41.70  
##                                                           Mean   :41.71  
##                                                           3rd Qu.:47.05  
##                                                           Max.   :56.20  
##       moe                  geometry 
##  Min.   :0.1000   MULTIPOLYGON :39  
##  1st Qu.:0.2000   epsg:4269    : 0  
##  Median :0.3000   +proj=long...: 0  
##  Mean   :0.5231                     
##  3rd Qu.:0.5500                     
##  Max.   :4.9000

See Julia Silge’s blog post about using leaflet with tinycensus.

# https://juliasilge.com/blog/using-tidycensus/

# Careful: colorNumeric vs. colorQuantile

# https://leaflet-extras.github.io/leaflet-providers/preview/
# ? colorNumeric
#   'palette = ' options: "viridis", "magma", "inferno", or "plasma".

wa_pal <- colorNumeric(palette = "plasma", domain = wa_age$estimate)

wa_age %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "CartoDB.Positron") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ wa_pal(estimate)) %>%
    addLegend("bottomright", 
              pal = wa_pal, 
              values = ~ estimate,
              title = "WA Median Age",
              opacity = 1)
fl_age <- get_acs(geography = "county", 
                     variables = "B01002_001", 
                     state = "FL",
                     geometry = TRUE) 
## Getting data from the 2012-2016 5-year ACS
#fl_age
fl_pal <- colorNumeric(palette = "plasma", domain = fl_age$estimate)

fl_age %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "CartoDB.Positron") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ fl_pal(estimate)) %>%
    addLegend("bottomright", 
              pal = fl_pal, 
              values = ~ estimate,
              title = "FL Median Age",
              opacity = 1)
us_age <- get_acs(geography = "state", 
                     variables = "B01002_001", 
                     geometry = TRUE) 
## Getting data from the 2012-2016 5-year ACS
# us_age
us_pal <- colorNumeric(palette = "viridis", domain = us_age$estimate)

us_age %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "CartoDB.Positron") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ us_pal(estimate)) %>%
    addLegend("bottomright", 
              pal = us_pal, 
              values = ~ estimate,
              title = "US Median Age",
              opacity = 1)