library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.4.2
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("7491cf955005ef7221778ebe1228115c53a12df6")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
library(ggmap)
library(leaflet)
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3

Locate Olympia

Google for the gps coordinates of a place. You can also click on a location in Google maps and see the coordinates.

oly = c(-122.9007,47.0379)
oly_12 = get_map(oly,zoom=12,scale=1)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=47.0379,-122.9007&zoom=12&size=640x640&scale=1&maptype=terrain&language=en-EN&sensor=false
ggmap(oly_12)

Exercise:

Review documentation of get_map in ggmap. Vary the parameters to understand how they change the map.

Get the data for the datacamp course from github.

Review the maps in “Insight through aesthetics.”

Read the Silge blog post.

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

Look at leaflet

https://rstudio.github.io/leaflet/

Repurpose the code in the blog to create the same maps for Washington State.

wa_pop <- get_acs(geography = "tract", 
                     variables = "B01003_001", 
                     state = "WA",
                     geometry = TRUE) 
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
class(wa_pop)
## [1] "sf"         "tbl_df"     "tbl"        "data.frame"
str(wa_pop)
## Classes 'sf', 'tbl_df', 'tbl' and 'data.frame':  1454 obs. of  6 variables:
##  $ GEOID   : chr  "53003960100" "53005011100" "53007960500" "53007961000" ...
##  $ NAME    : chr  "Census Tract 9601, Asotin County, Washington" "Census Tract 111, Benton County, Washington" "Census Tract 9605, Chelan County, Washington" "Census Tract 9610, Chelan County, Washington" ...
##  $ variable: chr  "B01003_001" "B01003_001" "B01003_001" "B01003_001" ...
##  $ estimate: num  4247 7957 6858 6451 1120 ...
##  $ moe     : num  294 573 866 654 215 262 326 382 202 357 ...
##  $ geometry:sfc_MULTIPOLYGON of length 1454; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:268, 1:2] -117 -117 -117 -117 -117 ...
##   ..- attr(*, "class")= chr  "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA
##   ..- attr(*, "names")= chr  "GEOID" "NAME" "variable" "estimate" ...
pal <- colorQuantile(palette = "viridis", domain = wa_pop$estimate, n = 10)

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