# Installing required packages

if (!require("dplyr")) install.packages("dplyr")
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("leaflet")) install.packages("leaflet")
if (!require("tidycensus")) install.packages("tidycensus")
if (!require("sf")) install.packages("sf")

library(dplyr)
library(tidyverse)
library(ggplot2) #From the tidyverse package
library(readr) #From the tidyverse package
library(leaflet)
library(tidycensus)
library(sf)
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")

###############################
#Single dad households
###############################

# Getting the map data
MyData <- get_acs(geography = "county",
                  state = "TN",
                  variables = c(MYVAR = "DP02_0007P"),
                  #Make CENSUSVAR match the Census variable name
                  year = 2022,
                  #Change 2021 to the year you prefer
                  survey = "acs1",
                  #Change acs1 to acs5 to get five-year data
                  output = "wide",
                  geometry = TRUE)

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

# Displaying the data
View(MyData)

# Error margin calculation
MyData$From <- MyData$MYVARE-MyData$MYVARM
MyData$To <- MyData$MYVARE+MyData$MYVARM
View(MyData)

# Graphing
ggplot(MyData, aes(x = MYVARE, y = reorder(NAME, MYVARE))) + 
  geom_errorbarh(aes(xmin = From, xmax = To)) + 
  geom_point(size = 3, color = "royalblue") + 
  theme_minimal(base_size = 12.5) + 
  labs(title = "Pct. Homes with Single Father and Children", 
       x = "Percent (blue dot) and error margin (bracket)", 
       y = "") + 
  scale_x_continuous()

# Making a preview map

MyData %>%
  ggplot(aes(fill = MYVARE)) + 
  geom_sf(color = NA) + 
  scale_fill_viridis_c(option = "magma") 

# Making a .kml Map File
MyData <- MyData[colSums(!is.na(MyData)) > 0]
st_write(MyData,"MapData.kml", append = FALSE)
## Writing layer `MapData' to data source `MapData.kml' using driver `KML'
## Writing 21 features with 6 fields and geometry type Multi Polygon.
# Making a CSV data file
MyData <- st_drop_geometry(MyData, drop = TRUE)
write_csv(MyData,"CSVData.csv")

###############################
#Single mom households
###############################


# Getting the map data
MyData <- get_acs(geography = "county",
                  state = "TN",
                  variables = c(MYVAR = "DP02_0011P"),
                  #Make CENSUSVAR match the Census variable name
                  year = 2022,
                  #Change 2021 to the year you prefer
                  survey = "acs1",
                  #Change acs1 to acs5 to get five-year data
                  output = "wide",
                  geometry = TRUE)

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

# Displaying the data
View(MyData)

# Error margin calculation
MyData$From <- MyData$MYVARE-MyData$MYVARM
MyData$To <- MyData$MYVARE+MyData$MYVARM
View(MyData)

# Graphing
ggplot(MyData, aes(x = MYVARE, y = reorder(NAME, MYVARE))) + 
  geom_errorbarh(aes(xmin = From, xmax = To)) + 
  geom_point(size = 3, color = "royalblue") + 
  theme_minimal(base_size = 12.5) + 
  labs(title = "Pct. Homes with Single Mother and Children", 
       x = "Percent (blue dot) and error margin (bracket)", 
       y = "") +   scale_x_continuous()

# Making a preview map

MyData %>%
  ggplot(aes(fill = MYVARE)) + 
  geom_sf(color = NA) + 
  scale_fill_viridis_c(option = "magma") 

# Making a .kml Map File
MyData <- MyData[colSums(!is.na(MyData)) > 0]
st_write(MyData,"MapData.kml", append = FALSE)
## Writing layer `MapData' to data source `MapData.kml' using driver `KML'
## Writing 21 features with 6 fields and geometry type Multi Polygon.
# Making a CSV data file
MyData <- st_drop_geometry(MyData, drop = TRUE)
write_csv(MyData,"CSVData.csv")