knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

cat("\014")
rm(list=ls())

knitr::opts_knit$set(root.dir = "C:/Users/dzhao/Downloads/DSCI 304")
library(readxl)
queueData = read_xlsx("Queues.xlsx", sheet = "Cleaned", col_types = c("text", "text", "numeric", "numeric", "numeric", "numeric", "numeric", "text", "text", "text", "text", "text", "text", "text", "numeric", "numeric", "text", "text", "text", "numeric"))

Texas = subset(queueData, state == "TX" & q_year == 2023)
Texas = Texas[!is.na(Texas$county) & !is.na(Texas$mw),]
Texas$mw = as.numeric(Texas$mw)

library(dplyr)

colSums(is.na(Texas))
##            q_id        q_status          q_date          q_year         ia_date 
##               0               0               0               0             349 
##         wd_date         on_date          entity         utility          county 
##             356             332               0               2               0 
##           state          region         service        poi_name       prop_date 
##               0               0               0               0               1 
##       prop_year   IA_status_raw IA_status_clean            type              mw 
##               1               0               0               0               0
Texas = Texas %>%
  group_by(county) %>%
  summarise(totalMW = sum(mw), na.rm = TRUE)
#Texas = aggregate(mw ~ county, data = Texas, FUN = sum)

library(sf)
library(ggpubr)
library(scales)
library(tidyr)
library(lubridate)

TX.map = st_read("TexasCounties")
## Reading layer `County' from data source 
##   `C:\Users\dzhao\Downloads\DSCI 304\TexasCounties' using driver `ESRI Shapefile'
## Simple feature collection with 254 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -11871800 ymin: 2978934 xmax: -10409240 ymax: 4369694
## Projected CRS: WGS 84 / Pseudo-Mercator
# ggplot(TX.map)+
#   geom_sf()

TX.map$county = TX.map$CNTY_NM

# Not all Texas counties had a project enter the queue in 2023 so we want to left join on the Texas counties dataframe so all of counties are included. We then set the counties that didn't have projects to 0 instead of NA
TexasProjects = merge(TX.map, Texas, by = "county", all.x = TRUE)
TexasProjects[is.na(TexasProjects)] = 0

map.theme = theme(axis.line=element_blank(),axis.text.x=element_blank(),
                 axis.text.y=element_blank(),axis.ticks=element_blank(),
                 axis.title.x=element_blank(),
                 axis.title.y=element_blank(),
                 panel.background=element_blank(),panel.border=element_blank(),
                 panel.grid.major=element_blank(),
                 panel.grid.minor=element_blank(),plot.background=element_blank())

TexasProjects$`Total Megawatts` = TexasProjects$totalMW

texasPlot = ggplot(data = TexasProjects)+
  geom_sf(aes(fill = totalMW, text = county, text2 = `Total Megawatts`), color = "black", size = 0.1)+
  scale_fill_gradient(name = "MW worth of Projects", low = "white", high = "dodgerblue4")+
  map.theme+
  labs(title = "Deaf Smith and Brazoria Counties Queue the Most Megawatts", subtitle = "Queue Year: 2023")+
  theme(legend.position = "none")+
  theme(title = element_text(hjust = 0.5))

library(plotly)

ggplotly(texasPlot, tooltip = c("text", "text2"))