# Installing required packages

if (!require("dplyr")) install.packages("dplyr")
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
if (!require("tidyverse")) install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.4
## ✔ ggplot2   3.4.1     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.1.8
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
if (!require("leaflet")) install.packages("leaflet")
## Loading required package: leaflet
if (!require("mapview")) install.packages("mapview")
## Loading required package: mapview
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")
## Loading required package: leaflet.extras2
if (!require("tidycensus")) install.packages("tidycensus")
## Loading required package: tidycensus
if (!require("sf")) install.packages("sf")
## Loading required package: sf
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)
library(tidyverse)
library(ggplot2) #From the tidyverse package
library(readr) #From the tidyverse package
library(leaflet)
library(tidycensus)
library(sf)
library(leaflet.extras2)
library(leafpop)

options(tigris_use_cache = TRUE)
options(scipen = 999)

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

#census_api_key("PasteYourAPIKeyBetwenTheseQuoteMarks")

# Getting the Year 1 data
Data_Y1 <- get_acs(geography = "county subdivision",
                   state = "TN",
                   variables = c(Var_Y1 = "B01001_001"),#Edit variable name
                   year = 2016,
                   survey = "acs5", #Note: Must use 5-year data
                   output = "wide",
                   geometry = TRUE)
## Getting data from the 2012-2016 5-year ACS
# Getting the Year 2 data
Data_Y2 <- get_acs(geography = "county subdivision",
                   state = "TN",
                   variables = c(Var_Y2 = "B01001_001"),#Edit variable name
                   year = 2021,
                   survey = "acs5", #Note: Must use 5-year data
                   output = "wide",
                   geometry = FALSE)
## Getting data from the 2017-2021 5-year ACS
counties <- "Davidson County|Rutherford County|Williamson County"

Data_Y2$SelectedCounty <- ifelse(grepl(counties,
                                      Data_Y2$NAME,
                                      ignore.case = TRUE),1,0)
Data_Y2 <- Data_Y2[Data_Y2$SelectedCounty == '1',]

# Merging the Year 1 and Year 2 data, using the GEOID variable
# in each dataset as a key
Mergeddata <- merge(Data_Y1, Data_Y2, by = "GEOID")
rm(Data_Y1,Data_Y2)

# Making pretty column names for the map file
Mergeddata$Subdivision <- Mergeddata$NAME.x
Mergeddata$Change <- Mergeddata$Var_Y2E - Mergeddata$Var_Y1E

# Testing the significance of each subdivision's change

Mergeddata$Sig <- significance(Mergeddata$Var_Y1E,
                               Mergeddata$Var_Y2E,
                               Mergeddata$Var_Y1M,
                               Mergeddata$Var_Y2M,
                               clevel = 0.90)
Mergeddata$Significance <- ifelse(Mergeddata$"Sig" == "TRUE",
                                  "Significant",
                                  "Nonsignificant")

# Sorting the data
Mergeddata <- Mergeddata[order(Mergeddata$Change, decreasing = TRUE),]

#Cutting unneeded columns and rearranging and renaming the data
ChangeData <- Mergeddata
ChangeData$From <- ChangeData$Var_Y1E
ChangeData$To <- ChangeData$Var_Y2E
keepvars <- c("GEOID","Subdivision",
              "From", "To",
              "Change","Significance",
              "geometry")
ChangeData <- ChangeData[keepvars]
rm(Mergeddata,
   keepvars)

# Displaying the data
View(ChangeData)

# Graphing
ggplot(ChangeData, aes(x = Change, y = reorder(Subdivision, Change))) + 
  geom_point(size = 3, color = "royalblue") + 
  theme_minimal(base_size = 12.5) + 
  labs(title = "Change in My Variable", 
       x = "Change", 
       y = "") + 
  scale_x_continuous()

# Making a map

mapviewOptions(basemaps.color.shuffle = FALSE)

PopMap <- mapview(ChangeData, zcol = "Change",
                  col.regions = RColorBrewer::brewer.pal(9, "Blues"), alpha.regions = .5,
                  layer.name = "Pop. change, 2016-2021",
                  popup = popupTable(ChangeData,
                                     feature.id = FALSE,
                                     row.numbers = FALSE,
                                     zcol = c("Subdivision",
                                              "From",
                                              "To",
                                              "Change",
                                              "Significance")))
## Warning: Found less unique colors (9) than unique zcol values (68)! 
## Interpolating color vector to match number of zcol values.
PopMap 
# Making a .kml Map File
ChangeData <- ChangeData[colSums(!is.na(ChangeData)) > 0]
st_write(ChangeData,"ChangeMapData.kml", append = FALSE)
## Writing layer `ChangeMapData' to data source `ChangeMapData.kml' using driver `KML'
## Writing 68 features with 6 fields and geometry type Multi Polygon.
# Making a CSV data file
ChangeDataNG <- st_drop_geometry(ChangeData, drop = TRUE)

ChangeDataNG <- ChangeDataNG %>% 
  separate(Subdivision,
           into = c("District","County","State"), sep = ",")
write_csv(ChangeDataNG,"ChangeCSVData.csv")

SummaryPop <- ChangeDataNG %>% 
  group_by(County, Significance) %>%
  summarise(PopSum = sum(To))
## `summarise()` has grouped output by 'County'. You can override using the
## `.groups` argument.
SummaryDistricts <- ChangeDataNG %>% 
  group_by(County, Significance) %>%
  summarise(Count_Div = n()) 
## `summarise()` has grouped output by 'County'. You can override using the
## `.groups` argument.