library(tidyverse)
library(readxl)
library(sf)
library(tmap)

Visualising GHG Emissions by Sector

# Loading dataset
g1 = read_csv("data.csv")
# Long data frame from wide
g2 = g1 %>% 
  pivot_longer(cols=2:33,names_to="year",values_to="emimmt") %>% 
  mutate(year = as.numeric(year)) %>% 
  rename(sector = 1) %>% 
  filter(sector != "Gross total")

# Preparing Area Chart, aes describes how the variables want to be displayed
ggplot(data=g2) +
  geom_area(aes(x=year,y=emimmt,fill=sector))

# Filtering data
g3 = g2 %>% 
  filter(year == 2021)

# Preparing Bar Chart
ggplot(data=g3) +
  geom_bar(aes(x=sector,y=emimmt,fill=sector), stat="identity")

GHG Emissions Analysis by States

# loading the detailed state ghg data
ghg1 = readRDS("stateghg2020-detailed.rds")

# finding all possible values of a column
unique(ghg1$econ_sector)
## [1] "Agriculture"             "Commercial"             
## [3] "Electric Power Industry" "Industry"               
## [5] "LULUCF Sector Net Total" "Residential"            
## [7] "Transportation"
# reading state and national population for years 1969-2021
statepop = readRDS("statepop21.rds")

# summing detailed emissions by economic sector
ghg2 = ghg1 %>% 
  select(stateabbr,year,econ_sector,emimmt) %>% 
  group_by(stateabbr,year,econ_sector) %>% 
  summarize(emimmt = sum(emimmt,na.rm=T)) %>% 
  left_join(statepop) %>% 
  mutate(mtperpopk = round((emimmt*10^6/pop)*1000,1))

# pulling Georgia data for 2020
ghgga = ghg2 %>% 
  filter(stateabbr == "GA" & year == 2020)

# calculating US totals
ghgus = ghg2 %>% 
  select(stateabbr,year,econ_sector,emimmt) %>% 
  group_by(year,econ_sector) %>% 
  summarize(emimmt = sum(emimmt,na.rm=T)) %>% 
  mutate(stateabbr = "US") %>% 
  left_join(statepop) %>% 
  mutate(mtperpopk = round((emimmt*10^6/pop)*1000,1))


# Mapping the results with tmap

# creating a GIS file of states for the coterminous US
sfstates = st_read("cb_2018_us_state_20m.shp") %>% 
  select(stateabbr = STUSPS) %>% 
  filter(!stateabbr %in% c("AK","HI","PR"))
## Reading layer `cb_2018_us_state_20m' from data source 
##   `/Users/apple/Desktop/Directory/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
# creating a 2020 Transportation sector dataframe for all states
stdata1= ghg2 %>% 
  filter(year == 2020 & econ_sector == "Transportation")  

# joining the 2020 Transportation data to the GIS file using the field stateabbr
sfstates1 = sfstates %>% 
  left_join(stdata1)

# mapping the result with tmap  
tm_shape(sfstates1) +
  tm_polygons("mtperpopk")

# creating 2020 Residential data for the states
stdata2= ghg2 %>% 
  filter(year == 2020 & econ_sector == "Residential")  

sfstates2 = sfstates %>% 
  left_join(stdata2)

tm_shape(sfstates2) +
  tm_polygons("mtperpopk")

# creating 2020 land use and forestry data for the states
stdata3= ghg2 %>% 
  filter(year == 2020 & econ_sector == "LULUCF Sector Net Total")  

sfstates3 = sfstates %>% 
  left_join(stdata3)

tm_shape(sfstates3) +
  tm_polygons("mtperpopk",
              palette = "-RdYlGn")

# switching tmap from the default static plot mode to interactive view mode
tmap_mode("view")

# adding alpha for 50% transparency of colors
# click on states to see their mtperpopk values
tm_shape(sfstates3) +
  tm_polygons("mtperpopk",
              palette = "-RdYlGn",
              alpha = .5)