# Installing required packages

if (!require("tidyverse")) install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
if (!require("tidycensus")) install.packages("tidycensus")
## Loading required package: tidycensus
if (!require("sf")) install.packages("sf")
## Loading required package: sf
## Warning: package 'sf' was built under R version 4.2.2
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
if (!require("mapview")) install.packages("mapview")
## Loading required package: mapview
## Warning: package 'mapview' was built under R version 4.2.2
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")
## Loading required package: leaflet.extras2
## Warning: package 'leaflet.extras2' was built under R version 4.2.2
## Loading required package: leaflet
library(tidycensus)
library(tidyverse)
library(sf)
library(mapview)
library(leaflet.extras2)
options(tigris_use_cache = TRUE)

# NOTE: Obtain a Census API key from 
# https://api.census.gov/data/key_signup.html
# and replace the census_api_key code line's 
# PastYourAPIKeyBetwenTheseQuoteMarks text with your
# API key. Also, delete the "#" from the front of the 
# census_api_key code.

#census_api_key("PasteYourAPIKeyBetwenTheseQuoteMarks")

# Getting ACS variable info
# Change "2021" to your preferred year.

DetailedTables <- load_variables(2021, "acs5", cache = TRUE)
SubjectTables <- load_variables(2021, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2021, "acs5/profile", cache = TRUE)
SubjectTables$geography <- "NA"
ProfileTables$geography <- "NA"
AllVariables <- rbind(DetailedTables,SubjectTables,ProfileTables)
rm(DetailedTables,SubjectTables,ProfileTables)

# Getting the map data

MapData1 <- get_acs(geography = "tract",
                    state = "TN",
                    county = "Rutherford",
                    variables = c(Pop = "B01001_001"),
                    year = 2016,
                    survey = "acs5",
                    output = "wide",
                    geometry = TRUE)
## Getting data from the 2012-2016 5-year ACS
MapData2 <- get_acs(geography = "tract",
                   state = "TN",
                   county = "Rutherford",
                   variables = c(Pop = "B01001_001"),
                   year = 2021,
                   survey = "acs5",
                   output = "wide",
                   geometry = TRUE)
## Getting data from the 2017-2021 5-year ACS
mapviewOptions(basemaps.color.shuffle = FALSE)

map2016 <- mapview(MapData1, zcol = "PopE", 
                   col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions = .5,
                   layer.name = "Pop. 2012-2016",
                   popup = FALSE
)
## Warning: Found less unique colors (9) than unique zcol values (49)! 
## Interpolating color vector to match number of zcol values.
map2021 <- mapview(MapData2, zcol = "PopE", 
                   col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions = .5,
                   layer.name = "Pop. 2017-2021",
                   popup = FALSE
)
## Warning: Found less unique colors (9) than unique zcol values (63)! 
## Interpolating color vector to match number of zcol values.
map2021 | map2016