library(tidyverse)
library(readxl)
library(sf)
library(tmap)
library(RColorBrewer)
library(viridis)
source("functions.R")
Load Complete SEDS
seds = read_csv("Complete_SEDS.csv") %>%
tolow3()
## Rows: 2007511 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): MSN, StateCode
## dbl (3): Data_Status, Year, Data
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Visualize the selected states
st = st_read("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
## `D:\S2\Climate Change Analytics\FinalProject\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
states_to_color <- c("NY", "CA", "GA", "FL")
st <- st %>%
mutate(color_state = ifelse(statecode %in% states_to_color, "Selected", "Not Selected"))
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(st) +
tm_polygons(col = "color_state", palette = c("Selected" = "lightgreen", "Not Selected" = "grey") , title = "Selected States")
Clean data
Filter data
seds1 = seds %>%
filter(msn %in% c("CLEIB", "NGEIB", "DFEIB", "TPOPP")) %>%
filter(statecode %in% c("GA","FL","CA","NY","US")) %>%
filter(year >= 1990)
Per capita calculation
seds2 = seds1 %>%
pivot_wider(names_from=msn,values_from=data) %>%
tolow() %>%
mutate(coalpercap = cleib/tpopp,
ngaspercap = ngeib/tpopp,
fuelpercap = dfeib/tpopp)
Consumption
Temporal change
ggplot(data=seds2) +
geom_line(aes(x=year,y=coalpercap, col=statecode),lwd=1.25) +
scale_color_brewer(palette = "Set3", name = "Region") +
labs(
title = "Coal per capita for electricity generation",
x = "Year",
y = "Coal per Capita (Billion Btu)"
)

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

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

Compare different sources use 2021 data
seds3 <- seds2 %>%
filter(year == 2021) %>%
pivot_longer(cols = c(coalpercap, ngaspercap, fuelpercap),
names_to = "type",
values_to = "data")
# Plotting
ggplot(seds3, aes(x = statecode, y = data, fill = type)) +
geom_bar(stat = "identity", position = "stack", width = 0.7) +
labs(
title = "Energy Per Capita for Electricity Generation",
x = "Region",
y = "Per Capita Value (Billion Btu)"
) +
scale_fill_manual(values = c("coalpercap" = "lightblue", "ngaspercap" = "lightgreen", "fuelpercap" = "red"),
name = "Energy Type",
breaks = c("coalpercap", "ngaspercap", "fuelpercap"),
labels = c("Coal", "Natural Gas", "Distillate fuel")) +
theme_minimal() +
theme(legend.position = "top", axis.text.x = element_text(angle = 45, hjust = 1))

seds31 <- seds2 %>%
mutate(total = coalpercap+ngaspercap+fuelpercap) %>%
mutate(coal_pct = coalpercap / total * 100,
ngas_pct = ngaspercap / total * 100,
fuel_pct = fuelpercap / total * 100)
Emissions
coef = read_csv("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))
## Rows: 13 Columns: 35
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): fuelname, fuelcode
## dbl (33): 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, ...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
seds4 = seds2 %>%
left_join(coef, by = "year") %>%
mutate(cemipercap = coalpercap*1000000*CL/1000000, # first convert to grams then to metric tons
nemipercap = ngaspercap*1000000*NG/1000000,
femipercap = fuelpercap*1000000*DF/1000000)
Temporal change
ggplot(data=seds4) +
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)"
)

ggplot(data=seds4) +
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)"
)

ggplot(data=seds4) +
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)"
)

Compare different sources use 2021 data
seds5 <- seds4 %>%
filter(year == 2021) %>%
pivot_longer(cols = c(cemipercap, nemipercap, femipercap),
names_to = "type",
values_to = "data")
ggplot(seds3, aes(x = statecode, y = data, fill = type)) +
geom_bar(stat = "identity", position = "dodge", width = 0.7) +
labs(
title = "Emissions Per Capita for Electricity Generation",
x = "Region",
y = "Per Capita Value (Metric tons)"
) +
scale_fill_manual(values = c("coalpercap" = "lightblue", "ngaspercap" = "lightgreen", "fuelpercap" = "red"),
name = "Energy Type",
breaks = c("coalpercap", "ngaspercap", "fuelpercap"),
labels = c("Coal", "Natural Gas", "Distillate fuel")) +
theme_minimal() +
theme(legend.position = "top", axis.text.x = element_text(angle = 45, hjust = 1))
