library(leaflet)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'tibble' was built under R version 3.4.4
## Warning: package 'purrr' was built under R version 3.4.4
## Warning: package 'dplyr' was built under R version 3.4.4
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(tidycensus)
library(leaflet)
library(stringr)
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
v15 <- load_variables(2016, "acs5", cache = TRUE)
View(v15)
#census_api_key("b24ce41117d6a656e716897a0b2bb1d5efec1fa6")
#census_api_key("install = TRUE")
Instructions: To complete this assignment, work the following problems and publish your completed work on RPubs.
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.
mi_med <- get_acs(geography = "tract",
variables = "B06011_001",
state = "MI",
county = "Kent County",
geometry = TRUE)
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Warning: package 'bindrcpp' was built under R version 3.4.4
pal <- colorNumeric(palette = "YlGnBu",
domain = mi_med$estimate)
mi_med %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "Esri.WorldImagery") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ pal(estimate)) %>%
addLegend("bottomright",
pal = pal,
values = ~ estimate,
title = "Median Income",
labFormat = labelFormat(prefix = "$"),
opacity = 1)
Reproduce your choropleth with the same color palette but a different provider.
mi_med %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "Thunderforest.SpinalMap") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ pal(estimate)) %>%
addLegend("bottomright",
pal = pal,
values = ~ estimate,
title = "Median Income",
labFormat = labelFormat(prefix = "$"),
opacity = 1)
Again: Reproduce your choropleth with the same color palette but a different provider.
mi_med %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "NASAGIBS.ViirsEarthAtNight2012") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ pal(estimate)) %>%
addLegend("bottomright",
pal = pal,
values = ~ estimate,
title = "Median Income",
labFormat = labelFormat(prefix = "$"),
opacity = 1)
Select one provider from the three that you’ve tried. Try a different color palette from a different source.
pal2 <- colorNumeric(palette = "viridis",
domain = mi_med$estimate)
mi_med %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "Thunderforest.SpinalMap") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.9,
color = ~ pal2(estimate)) %>%
addLegend("bottomright",
pal = pal2,
values = ~ estimate,
title = "Median Income",
labFormat = labelFormat(prefix = "$"),
opacity = 1)
Again, stick with the current provider, but try a color palette from a third source.
pal3 <- colorNumeric(palette = "Greens",
domain = mi_med$estimate)
mi_med %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "Thunderforest.SpinalMap") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.9,
color = ~ pal3(estimate)) %>%
addLegend("bottomright",
pal = pal3,
values = ~ estimate,
title = "Median Income",
labFormat = labelFormat(prefix = "$"),
opacity = 1)
From the providers and color palettes, which you have tried, which one do you prefer?
I like the Viridis palette because it is actually legible with the reds/oranges of the SpinalMap
No code is required.