Libraries

library(tidycensus)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(crsuggest)
## Using the EPSG Dataset v10.019, a product of the International Association of Oil & Gas Producers. 
## Please view the terms of use at https://epsg.org/terms-of-use.html.

Load Census API Key (Hidden)

## To install your API key for use in future sessions, run this function with `install = TRUE`.

ACS Variables

v19 <- load_variables(2019, "acs5", cache = TRUE)
#View(v19)

v19 %>% filter(grepl('veteran', label, ignore.case = TRUE))

PA Data

options(tigris_use_cache = TRUE)
centre_acs_map_data <- get_acs(state = "PA", county = "Centre", geography = "tract", variables = "B21001_002", geometry = TRUE)
## Getting data from the 2015-2019 5-year ACS
#pa_acs_B21001_002_map_data <- get_acs(state = "PA", geography = "county", variables = "B21001_002", geometry = TRUE)

pa_acs_map_data <- c()
acs_variables_of_interest <- c("B21001_002", "B21002_002", "B21002_016", "B21005_007", "B21005_025", "B21100_003", "B21002_012",
                               "B21002_011", "B21002_007", "B21002_008")

# default survey = acs5 ("acs1", "acs3", and "acs5") meaning 2015-2019 5-year estimates
for (acsvar in acs_variables_of_interest) {
  pa_acs_map_data[[acsvar]] <- get_acs(state = "PA", geography = "county", variables = acsvar, geometry = TRUE)
}
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS
## Getting data from the 2015-2019 5-year ACS

Map

# Find suggested CRS
#suggest_crs(centre)

#centre_acs_map_data %>%
#  ggplot(aes(fill = estimate)) + 
#  geom_sf(color = NA) + 
#  coord_sf(crs = 6565) + 
#  scale_fill_viridis_c() 

for (mapvar in pa_acs_map_data) {

  # Lookup ACS Variable Info
  mapvar_acs <- v19[v19$name == mapvar$variable[1], ]
  
  # Map
  print(
    mapvar %>%
      ggplot(aes(fill = estimate)) + 
      geom_sf(color = NA) + 
      coord_sf(crs = 6565) + 
      scale_fill_viridis_c() +
      labs(title = mapvar_acs$name, 
           subtitle = mapvar_acs$label, 
           caption = "U.S. Census American Community Survey 5-year Estimates (2015-2019)")
  )
}

library(mapview)
mapview(centre_acs_map_data, zcol = "estimate", layer.name = "Centre County - Estimate!!Total:!!Veteran - B21001_002")
for (mapv in pa_acs_map_data) {
  mapv_acs <- v19[v19$name == mapv$variable[1], ]
  print(
    mapview(mapv, zcol = "estimate", layer.name = mapv_acs$label)
  )
}