library(tidyverse)
library(readxl)
library(sf)
library(tmap)
library(RColorBrewer)
library(viridis)
library(dplyr)
library(viridis)
library(hrbrthemes)
source("E:/CP8803climate change/hwork0423/functions.R")
#load the dataset
seds1 = read_csv("E:/CP8803climate change/hwork0423/Complete_SEDS.csv") %>% 
  tolow3()
stateinfo1 = read_excel("E:/CP8803climate change/hwork0423/stateinfo6.xls")
st1 = st_read("E:/CP8803climate change/hwork0423/cb_2018_us_state_20m.shp") %>% 
  tolow3() %>% 
  mutate(statecode = stusps) %>% 
  filter(statecode != "AK" & 
           statecode != "HI" &
           statefp <= "56") %>% 
  mutate(stateabbr = statecode)
## Reading layer `cb_2018_us_state_20m' from data source 
##   `E:\CP8803climate change\hwork0423\cb_2018_us_state_20m.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 52 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.1743 ymin: 17.91377 xmax: 179.7739 ymax: 71.35256
## Geodetic CRS:  NAD83
tmap_mode("view")
## tmap mode set to interactive viewing
#Show the selected states
states_to_color <- c("FL", "MN", "GA", "NY")

# Create a new column for colors
st1 <- st1 %>%
  mutate(color = ifelse(statecode %in% states_to_color, "Chosen", "None"))

# Create the map using the colors
tm_shape(st1) +
  tm_polygons(col = "color", title = "Chosen States")
#Tidy the data
seds2 = seds1 %>% 
  filter(msn %in% c("CLEIB", "NGEIB", "DFEIB", "TPOPP")) %>%
  filter(statecode %in% c("FL", "MN", "GA", "NY","US")) %>%
  filter(year >= 1990,year <=2022)
# Caculate the per capita fuel use
seds3 = seds2 %>% 
  pivot_wider(names_from=msn,values_from=data) %>% 
  tolow() %>%
  mutate(coalpercap = cleib/tpopp,
         ngaspercap = ngeib/tpopp,
         fuelpercap = dfeib/tpopp)

Follow the time change #Visualization of Coal per capita for electricity energy use

ggplot(data=seds3) +
  geom_line(aes(x=year,y=coalpercap, col=statecode),lwd=1.25) +
  scale_color_brewer(palette = "Set2", name = "Chosen State") +
  labs(
    title = "Coal per capita for electricity energy use",
    x = "Year", 
    y = "Coal per Capita (Billion Btu) "
  )

These lines show an overall downward trend in per capita coal use for all states and the U.S. as a whole, suggesting that reliance on coal for per capita electricity generation declined during the 1990 to 2020. Some lines show more dramatic declines than others, which could be a result of a shift to alternative energy sources or other socioeconomic factors affecting coal consumption in these areas.

#Visualization of Natural gas per capita for electricity energy use

ggplot(data=seds3) +
  geom_line(aes(x=year,y=ngaspercap, col=statecode),lwd=1.25) +
  scale_color_brewer(palette = "Set3", name = "Chosen State") +
  labs(
    title = "Natural gas per capita for electricity energy use",
    x = "Year", 
    y = "Natural gas per Capita (Billion Btu) "
  )

In contrast to the previous chart on coal use, the lines here show an overall increasing trend in per capita natural gas consumption by state and across the United States. This indicates an increasing reliance on natural gas for per capita electricity generation over the observation period.

#Visualization of Distillate fuel per capita for electricity energy use

ggplot(data=seds3) +
  geom_line(aes(x=year,y=fuelpercap, col=statecode),lwd=1.25) +
  scale_color_brewer(palette = "Set3", name = "Chosen State") +
  labs(
    title = "Distillate fuel per capita for electricity energy use",
    x = "Year", 
    y = "Distillate fuel per Capita(Billion Btu) "
  )

The graph shows significant fluctuations in per capita distillate use. It is interesting to note that there are peaks and valleys in consumption fluctuations over the years, which could be attributed to a variety of factors, but by the end of the period all the lines seem to converge and level off, suggesting that per capita distillate use is leveling off across the states and the nation. This could signal a stabilization of consumption patterns or a transition to alternative energy generation.

Comparing chosen areas sources used in 2021 years

seds4 <- seds3 %>%
  filter(year == 2021) %>%
  pivot_longer(
    cols = c("coalpercap", "ngaspercap", "fuelpercap"),
    names_to = "type",
    values_to = "data"
  )

# Update factor levels with the desired labels
seds4$type <- factor(seds4$type, levels = c("coalpercap", "ngaspercap", "fuelpercap"),
                     labels = c("Coal", "Natural Gas", "Distillate fuel"))
# Create the plot
ggplot(seds4, aes(x = statecode, y = data, fill = type)) + 
  geom_bar(stat = "identity", position = "stack") +
  scale_fill_manual(
    values = c("Coal" = "pink", "Natural Gas" = "grey", "Distillate fuel" = "red"),
    name = "Energy Type"
  ) +
  labs(
    title = "Energy Per Capita for Electricity Generation",
    x = "Region",
    y = "Per Capita Value (Billion Btu)"
  ) +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    plot.title = element_text(size = rel(1.1))  
  )

The figure shows that per capita consumption of natural gas is generally higher than consumption of coal and distillate fuels in every region except Minnesota, where coal use is slightly higher. New York State has the lowest total per capita energy consumption of the states shown, while the United States has the highest total per capita energy consumption, suggesting that the nation as a whole has higher total per capita energy consumption for electricity generation.

seds5 <- seds3 %>%
  mutate(total = coalpercap+ngaspercap+fuelpercap) %>%
  mutate(coal_pct = coalpercap / total * 100,
         ngas_pct = ngaspercap / total * 100,
         fuel_pct = fuelpercap / total * 100)
#Calculate the emissions
coef = read_csv("E:/CP8803climate change/coef-revised-23b.csv") %>%
  filter(fuelcode %in% c("CL","NG","DF")) %>%
  select(-"fuelname") %>%
  pivot_longer(cols = -(fuelcode), names_to = "year", values_to = "coef") %>%
  pivot_wider(names_from = fuelcode, values_from = coef) %>%
  mutate(year = as.numeric(year))

#Join two tables

seds6 = seds3 %>%
  left_join(coef, by = "year") %>%
  mutate(cemipercap = coalpercap*1000000*CL/1000000, 
         nemipercap = ngaspercap*1000000*NG/1000000,
         femipercap = fuelpercap*1000000*DF/1000000)

Follow the time change #Coal emissions per capita for electricity generation

ggplot(data=seds6) +
  geom_line(aes(x=year,y=cemipercap, col=statecode),lwd=1.25) +
  scale_color_brewer(palette = "Set3", name = "Region") +
  labs(
    title = "Coal emissions per capita for electricity generation",
    x = "Year", 
    y = "Emissions (Metric tons)"
  )

Over the years, each region has shown a clear trend in per capita coal emissions. GA and US started out with relatively high per capita emissions, which declined significantly over time, with some fluctuations. MN shows a gradual decrease in emissions throughout the period. FL shows a consistent decline, reaching relatively low levels by the end of the period. NY maintains the lowest per capita emissions throughout the period and shows a steady downward trend that levels off near the end. Overall, all regions show a clear downward trend in per capita coal emissions, especially after 2005.

#Natural gas emissions per capita for electricity generation

ggplot(data=seds6) +
  geom_line(aes(x=year,y=nemipercap, col=statecode),lwd=1.25) +
  scale_color_brewer(palette = "Set3", name = "Region") +
  labs(
    title = "Natural gas emissions per capita for electricity generation",
    x = "Year", 
    y = "Emissions (Metric tons)"
  )

FL shows a significant increase in per capita emissions during this period and a sharp rise after the early 2000s. GA and NY also show an upward trend, but with greater volatility and a smaller slope than FL. Per capita emissions in US have gradually increased, albeit at a lower level compared to other states. MN has steadily increasing per capita emissions, indicating a national trend of increasing per capita natural gas use. Overall, the graph shows that per capita natural gas emissions, unlike coal emissions, are generally increasing, which may reflect a shift in energy production from coal to natural gas, which is considered a cleaner alternative to fossil fuels.

#Distillate fuel emissions per capita for electricity generation

ggplot(data=seds6) +
  geom_line(aes(x=year,y=femipercap, col=statecode),lwd=1.25) +
  scale_color_brewer(palette = "Set3", name = "Region") +
  labs(
    title = "Distillate fuel emissions per capita for electricity generation",
    x = "Year", 
    y = "Emissions (Metric tons)"
  )

All regions show significant fluctuations over the years. Florida shows particularly high peaks, indicating a period of particularly high per capita distillate emissions, especially in the early 2000s. GA, NY, and US emissions have generally declined since the early 2000s, with greater declines in GA and US. MN, while starting from a lower base, also saw a peak in the early 2000s and then followed a downward trend similar to the other regions. After 2010, all regions show a downward trend, indicating a decrease in per capita distillate fuel emissions for electricity generation. Overall, the figure shows that while there has been considerable variation in per capita distillate fuel emissions over the past three decades, the recent trend in these regions has been toward lower per capita emissions from this fuel source in electricity generation.

Compare different sources use 2021 data

seds7 <- seds6 %>%
  filter(year == 2021) %>%
  pivot_longer(
    cols = c("coalpercap", "ngaspercap", "fuelpercap"),
    names_to = "type",
    values_to = "data"
  )
# Update the levels of the 'type' factor to have meaningful labels
seds7$type <- factor(seds7$type, levels = c("coalpercap", "ngaspercap", "fuelpercap"),
                     labels = c("Coal", "Natural Gas", "Distillate Fuel"))
#let's plot
ggplot(seds7, aes(x = statecode, y = data, fill = type)) +
  geom_bar(stat = "identity", position = "stack") + # Ensure bars are stacked
  scale_fill_manual(
    values = c("Coal" = "pink", "Natural Gas" = "grey", "Distillate Fuel" = "red"),
    name = "Energy Type"
  ) +
  labs(
    title = "Energy Per Capita for Electricity Generation",
    x = "State",
    y = "Per Capita Value (Billion Btu)"
  ) +
  theme_minimal() +
  theme(
    legend.position = "bottom"
  )

Florida has the highest total per capita energy consumption, with natural gas contributing the most, followed by coal and a smaller share of distillate fuels. GA does not have a large distribution gap between coal and natural gas, with a smaller share of distillate fuels. MN shows that the majority of per capita energy consumption comes from coal, a small amount from natural gas, and only a very small amount from distillate fuels. Of the states shown, New York has the lowest total per capita energy consumption, most of which comes from natural gas. The bar chart for the United States, reflecting the national average, shows that a large portion of per capita energy consumption comes from natural gas, followed by coal, with distillate fuels making up the smallest share. The chart highlights differences in energy consumption patterns by region, with Florida and New York relying significantly on natural gas and Minnesota relying more on coal. It also shows the different roles distillate fuels play in each region’s energy mix. The overall trend suggests that natural gas will become a significant source of per capita energy consumption for electricity generation.