library(tidyverse)
library(janitor)
library(scales)
library(paletteer)

CONSUMPTION <- read_csv('EIA_Commerical_Sector_Data.csv',skip=4) %>% clean_names()
CONSUMPTION[,c(2:14)] <- 293.07* CONSUMPTION[,c(2:14)]
colnames(CONSUMPTION) <- gsub("_quads","",colnames(CONSUMPTION))
CONSUMPTION <- CONSUMPTION %>% pivot_longer(-year,values_to='TWh',names_to="Use")
TOTAL <- CONSUMPTION %>% filter(Use=='total_gross_end_use_consumption') 
CONSUMPTION <-CONSUMPTION %>% filter(Use!='total_gross_end_use_consumption') 


CONSUMPTION$Use <- ifelse(CONSUMPTION$Use!='data_center_servers',"Other","Data Centers")
CONSUMPTION <- CONSUMPTION %>% group_by(year,Use) %>% summarize(TWh=sum(TWh),.groups = "drop_last") %>% ungroup
CONSUMPTION$Use <- factor(CONSUMPTION$Use,level=c("Other","Data Centers")) 

CONSUMPTION <- CONSUMPTION %>% group_by(Use) %>% mutate(Change=TWh-sum(ifelse(year==2025,TWh,0))) %>% ungroup

STACK_PLOT <- ggplot(CONSUMPTION,aes(x=year,y=Change,fill=Use)) +geom_area()+theme_bw()+facet_wrap(~Use)+ theme(legend.position = "top",text = element_text(size = 20))+scale_fill_manual(values=paletteer_d("PNWColors::Shuksan2"))+scale_y_continuous(breaks=seq(-600,600,by=50))+ylab("Change TW hours")

STACK_TOTAL<- ggplot(CONSUMPTION,aes(x=year,y=TWh,fill=Use)) +geom_area()+theme_bw()+ theme(legend.position = "top",text = element_text(size = 20))+scale_fill_manual(values=paletteer_d("PNWColors::Shuksan2"))+scale_y_continuous(breaks=seq(0,6000,by=500))+ylab("Total Consumpiotn (TW hours)")

ggsave("../Plot.png",STACK_PLOT,width=12,height=8,units="in",dpi=600)
ggsave("../Total.png",STACK_TOTAL,width=12,height=8,units="in",dpi=600)

