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)
# 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)

Instructions: To complete this assignment, work the following problems and publish your completed work on RPubs.

Problem 1

Select a leaflet provider and a color palette to construct a choropleth of median income by census tract in a US county of your choice.

LaneOR_income <- get_acs(geography = "tract", 
                     variables = "B19013_001", 
                     state = "OR",
                     county = "Lane",
                     geometry = TRUE) 
## Getting data from the 2012-2016 5-year ACS
LaneOR_income
## Simple feature collection with 87 features and 5 fields (with 1 geometry empty)
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.1587 ymin: 43.43739 xmax: -121.7681 ymax: 44.29054
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
## First 10 features:
##          GEOID                                   NAME   variable estimate
## 1  41039000100    Census Tract 1, Lane County, Oregon B19013_001    53942
## 2  41039000200    Census Tract 2, Lane County, Oregon B19013_001    69583
## 3  41039000300    Census Tract 3, Lane County, Oregon B19013_001    62552
## 4  41039000402 Census Tract 4.02, Lane County, Oregon B19013_001    61422
## 5  41039000403 Census Tract 4.03, Lane County, Oregon B19013_001    45655
## 6  41039000404 Census Tract 4.04, Lane County, Oregon B19013_001    48866
## 7  41039000500    Census Tract 5, Lane County, Oregon B19013_001    39405
## 8  41039000702 Census Tract 7.02, Lane County, Oregon B19013_001    46250
## 9  41039000705 Census Tract 7.05, Lane County, Oregon B19013_001    31517
## 10 41039000706 Census Tract 7.06, Lane County, Oregon B19013_001    36698
##      moe                       geometry
## 1   7288 MULTIPOLYGON (((-122.8652 4...
## 2   2601 MULTIPOLYGON (((-123.0468 4...
## 3  10108 MULTIPOLYGON (((-123.1654 4...
## 4  10011 MULTIPOLYGON (((-123.5009 4...
## 5   7579 MULTIPOLYGON (((-123.2896 4...
## 6   6438 MULTIPOLYGON (((-123.2487 4...
## 7   4580 MULTIPOLYGON (((-124.0142 4...
## 8   7932 MULTIPOLYGON (((-124.1503 4...
## 9   3829 MULTIPOLYGON (((-124.1253 4...
## 10  8918 MULTIPOLYGON (((-124.1369 4...
p1 <- colorNumeric(palette = "plasma", domain = LaneOR_income$estimate)

LaneOR_income %>%
    na.omit()  %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "OpenStreetMap.HOT") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ p1(estimate)) %>%
    addLegend("bottomright", 
              pal = p1, 
              values = ~ estimate,
              title = "Median Income",
              opacity = 1) 

Problem 2

Reproduce your choropleth with the same color palette but a different provider.

LaneOR_income %>%
  na.omit()  %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "Esri.NatGeoWorldMap") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ p1(estimate)) %>%
    addLegend("bottomright", 
              pal = p1, 
              values = ~ estimate,
              title = "Median Income",
              opacity = 1) 

Problem 3

Again: Reproduce your choropleth with the same color palette but a different provider.

LaneOR_income %>%
    na.omit()  %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "HikeBike.HikeBike") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ p1(estimate)) %>%addLegend("bottomright", 
              pal = p1, 
              values = ~ estimate,
              title = "Median Income",
              opacity = 1) 
# https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/colorPaletteCheatsheet.pdf

library(RColorBrewer)

display.brewer.all(n=NULL, type="all", select=NULL, exact.n=TRUE, 
colorblindFriendly=FALSE)

# https://cran.r-project.org/web/packages/colorspace/vignettes/hcl-colors.pdf
library(colorspace)

# p2 <- colorNumeric(palette = "Spectral", domain = LaneOR_income$estimate)
# p2 <- colorNumeric(palette = "Blues", domain = LaneOR_income$estimate)
# p2 <- colorNumeric(palette = rainbow_hcl(7), domain = LaneOR_income$estimate)
# p2 <- colorNumeric(palette = "Oranges", domain = LaneOR_income$estimate)

Problem 4

Select one provider from the three that you’ve tried. Try a different color palette from a different source.

# Palette from RColorBrewer
p2 <- colorNumeric(palette = "BuPu", domain = LaneOR_income$estimate)


LaneOR_income %>%
  na.omit()  %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "Esri.NatGeoWorldMap") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ p2(estimate)) %>%
    addLegend("bottomright", 
              pal = p2, 
              values = ~ estimate,
              title = "Median Income",
              opacity = 1) 

Problem 5

Again, stick with the current provider, but try a color palette from a third source.

# Palette from colorspace
p3 <- colorNumeric(palette = rainbow_hcl(7), domain = LaneOR_income$estimate)


LaneOR_income %>%
  na.omit()  %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "Esri.NatGeoWorldMap") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ p3(estimate)) %>%
    addLegend("bottomright", 
              pal = p3, 
              values = ~ estimate,
              title = "Median Income",
              opacity = 1)  

Problem 6

From the providers and color palettes, which you have tried, which one do you prefer?

I think I like #2 the best. I also like #4 but there’s not much contrast between the blues in the palette and the water in the map.