v ggplot2 3.3.5 v purrr 0.3.4
v tibble 3.1.8 v dplyr 1.0.7
v tidyr 1.1.4 v stringr 1.4.0
v readr 2.1.1 v forcats 0.5.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
Code
library(janitor)
Attaching package: 'janitor'
The following objects are masked from 'package:stats':
chisq.test, fisher.test
Code
library(here)
here() starts at C:/Users/Ryan/Documents/Sustainable Finance Assignment 2
Code
library(readxl)library(tidyr)library(dplyr)library(rnaturalearth)library(countrycode)library(wbstats)library(ggplot2)library(gganimate)#install.packages('gganimate')library(gapminder)#install.packages('gifski')library(gifski)#Here, I read in data for domestic emissions from 1990 to 2018.scope_1_domestic <-read_csv("~/Raw Data Finace Project 2/Scope 1 Emissions.csv")
Rows: 193 Columns: 31
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (3): Country/Region, unit, 1990
dbl (28): 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, ...
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
#I omit empty data entries because they have no value at this point.scope_1_domestic <- scope_1_domestic %>%na.omit()#I change data type so that emissions in the year 1990 are seen as numerical and not categorical. This will allow me to better sort the data later.scope_1_domestic$"1990"<-as.numeric(as.character(scope_1_domestic$"1990")) # Convert one variable to numeric#I clean the data so that year is one column and domestic emissions are one column.scope_1_domestic <- scope_1_domestic %>%pivot_longer("1990":"2018",names_to ="year",values_to ="domestic_emissions" )#I rename the data so that it is standardized.scope_1_domestic <- scope_1_domestic %>%rename("country_name"="Country/Region")#Create country code index function.country_name_regex_to_iso3c <-function(country_name) { country_name |>countrycode(origin ="country.name", destination ="iso3c",origin_regex =TRUE)}#Create iso3c index function.iso3c_to_country_name <-function(iso3c) { iso3c |>countrycode(origin ="iso3c", destination ="country.name")}#I add a standardized country name and ISO code for each country.scope_1_domestic <- scope_1_domestic %>%mutate(iso3c =country_name_regex_to_iso3c(country_name)) |>mutate(country_name =iso3c_to_country_name(iso3c)) #Download data for export emissions for each country from 1995-2018.scope_1_export <-read_excel("~/Raw Data Finace Project 2/Export Emissions!.xlsx", skip =6)#I clean the data so that year is one column and export emissions are one column. scope_1_export <- scope_1_export %>%pivot_longer("1995":"2018",names_to ="year",values_to ="export_emissions" )#Standardize column names, scope_1_export <- scope_1_export %>%rename("country_name"="Country")#Standardize country names and add country code. scope_1_export <- scope_1_export %>%mutate(iso3c =country_name_regex_to_iso3c(country_name)) |>mutate(country_name =iso3c_to_country_name(iso3c)) #Delete empty data entries. scope_1_export <- scope_1_export %>%na.omit()#Join domestic emissions and export emissions so that I now have all scope 1 emissions data. scope_1_and_export <-full_join(scope_1_domestic, scope_1_export, by =c("year"="year", "country_name"="country_name", "iso3c"="iso3c"))#Read in scope 2 emissions info from data base. scope_2 <-read_excel("~/Raw Data Finace Project 2/Scope 2 Emissions.xlsx", skip =6)#I clean the data so that year is one column and scope 2 emissions are one column. scope_2 <- scope_2 %>%pivot_longer("1995":"2018",names_to ="year",values_to ="scope_2_emissions" )#Standardize column and country names and add ISO index. scope_2 <- scope_2 %>%rename("country_name"="Country")scope_2 <- scope_2 %>%mutate(iso3c =country_name_regex_to_iso3c(country_name)) |>mutate(country_name =iso3c_to_country_name(iso3c)) #Omit missing datascope_2 <- scope_2 %>%na.omit()#Download scope 3 emissions data.scope_3 <-read_excel("~/Raw Data Finace Project 2/Scope 3 Emissions.xlsx", skip =6)#rearrange data so that year is in a column and scope 3 emissions are in a column.scope_3 <- scope_3 %>%pivot_longer("1995":"2018",names_to ="year",values_to ="scope_3_emissions" )#Standardize country names and column names and add ISO index.scope_3 <- scope_3 %>%rename("country_name"="Country")scope_3 <- scope_3 %>%mutate(iso3c =country_name_regex_to_iso3c(country_name)) |>mutate(country_name =iso3c_to_country_name(iso3c)) #Remove missing data.scope_3 <- scope_3 %>%na.omit()#combine scope 1 and scope 2 emissions data.scopes_1_and_2 <-full_join(scope_1_and_export, scope_2, , by =c("year"="year", "country_name"="country_name", "iso3c"="iso3c"))#Combine scope 1, 2 and 3 emissions data into one data base.all_scopes_all_data <-full_join(scopes_1_and_2, scope_3, , by =c("year"="year", "country_name"="country_name", "iso3c"="iso3c"))#Create column with total emissions where I add scope 1, 2, and 3, emissions.all_scopes_all_data <- all_scopes_all_data %>%mutate(consumption_emissions = domestic_emissions + scope_2_emissions + scope_3_emissions - export_emissions)#Remove unnecessary columns.all_scopes_all_data <-select(all_scopes_all_data, -unit)#Download GDP PPP data.GDP_PPP <-read_excel("~/Raw Data Finace Project 2/GDP_by_PPP.xlsx", skip =3)#Rearrange data so that year is a column and GDP PPP is a column.GDP_PPP <- GDP_PPP %>%pivot_longer("1960":"2021",names_to ="year",values_to ="GDP_PPP_dollars" )#Remove missing data.GDP_PPP <- GDP_PPP %>%na.omit()#Standardize column names and remove unnecessary columns.GDP_PPP <- GDP_PPP %>%rename("country_name"="Country Name")GDP_PPP <- GDP_PPP %>%rename("iso3c"="Country Code")GDP_PPP <-select(GDP_PPP, -"Indicator Name")GDP_PPP <-select(GDP_PPP, -"Indicator Code")#Add GDP PPP to data base with scope 1, 2, and 3 emissions.all_data_scopes_GDP_PPP <-full_join(all_scopes_all_data, GDP_PPP, , by =c("year"="year", "country_name"="country_name", "iso3c"="iso3c"))#Download nominal GDP data.nom_GDP <-read_excel("~/Raw Data Finace Project 2/Nominal GDP.xlsx", skip =3)#Rearrange it so that year is a column and nominal GDP is a columnnom_GDP <- nom_GDP %>%pivot_longer("1960":"2021",names_to ="year",values_to ="nom_GDP_dollars" )#Omit missing datanom_GDP <- nom_GDP %>%na.omit()#Standardize column names and remove unnecessary columns.nom_GDP <- nom_GDP %>%rename("country_name"="Country Name")nom_GDP <- nom_GDP %>%rename("iso3c"="Country Code")nom_GDP <-select(nom_GDP, -"Indicator Name")nom_GDP <-select(nom_GDP, -"Indicator Code")#Add nominal GDP tp data set with scopes 1-3 emissions and GDP PPP.all_data_scopes_GDP_PPP_and_nom_na <-full_join(all_data_scopes_GDP_PPP, nom_GDP, , by =c("year"="year", "country_name"="country_name", "iso3c"="iso3c"))debt_pct_gdp <-read_excel("~/Raw Data Finace Project 2/Government Debt.xlsx")debt_pct_gdp <- debt_pct_gdp %>%pivot_longer("1950":"2021",names_to ="year",values_to ="pct_debt_gdp" )debt_pct_gdp <- debt_pct_gdp %>%na.omit()debt_pct_gdp <- debt_pct_gdp %>%rename("country_name"="Country")debt_pct_gdp <- debt_pct_gdp %>%mutate(iso3c =country_name_regex_to_iso3c(country_name)) |>mutate(country_name =iso3c_to_country_name(iso3c)) all_data_debt_no_na <-full_join(all_data_scopes_GDP_PPP_and_nom_na, debt_pct_gdp, , by =c("year"="year", "country_name"="country_name", "iso3c"="iso3c"))all_data_debt_no_na <- all_data_debt_no_na %>%mutate(debt = (pct_debt_gdp/100) * nom_GDP_dollars)iso3c_to_x <- purrr::partial(countrycode, origin ="iso3c")all_data_debt_no_na <- all_data_debt_no_na %>%mutate(region =iso3c_to_x(iso3c, destination ="region"))all_data_debt_no_na <- all_data_debt_no_na %>%mutate(scope_1_emissions_per_dollar_gdp_ppp = domestic_emissions/GDP_PPP_dollars)all_data_debt_no_na <- all_data_debt_no_na %>%mutate(consumption_emissions_per_dollar_gdp_ppp = consumption_emissions /GDP_PPP_dollars)#millions on tons of CO2/dollarall_data_debt_no_na <- all_data_debt_no_na %>%drop_na(region)all_data_debt_no_na <- all_data_debt_no_na %>%mutate(financed_emission = (debt/GDP_PPP_dollars) * consumption_emissions)financed_emissions_no_na <- all_data_debt_no_na %>%drop_na(financed_emission)
Code
all_data_debt_no_na %>%drop_na(consumption_emissions, debt) %>%ggplot(aes(x = consumption_emissions, y = debt/1000000)) +geom_point(na.rm =TRUE) +geom_smooth(na.rm =TRUE) +facet_wrap(~region, scales ="free_x", "free_y") +labs(title ="Figure 1: Sovereign Debt v. Consumption Emissions by Region",subtitle =str_wrap("Regions have very different relationships between debt and emissions, but there is a somewhat positive relationshsip between the two.", width =70),x ="Conusmption Emissions (millions of tons of CO2)",y ="Sovereign Debt (USD)", caption ="Sources: Climate Watch, OECD, IMF, World Bank | Latest Data: 2018 | Calculations by Ryan Showman" ) +scale_y_continuous(labels = scales::label_dollar(suffix ="M"), expand =c(0,0)) +theme_minimal()
Code
all_data_debt_no_na %>%drop_na(consumption_emissions, debt) %>%ggplot(aes(x = consumption_emissions, y = debt/1000000, color = region)) +geom_point(na.rm =TRUE, alpha =1/5) +labs(title ="Figure 2: Sovereign Debt v. Consumption Emissions by Region",subtitle =str_wrap("Some regions have somewhat constant debt no matter their emissions, while others have a more linear relationship between debt and emissions. ", width =70),x ="Conusmption Emissions (millions of tons of CO2)",y ="Sovereign Debt (USD)", caption ="Sources: Climate Watch, OECD, IMF, World Bank | Latest Data: 2018 | Calculations by Ryan Showman" ) +scale_y_continuous(labels = scales::label_dollar(suffix ="M"), expand =c(0,0)) +theme_minimal()
Code
financed_emissions_2018 <- financed_emissions_no_na %>%filter(year =="2018")# get the map data in sf format (google it)world_map_data <- rnaturalearth::ne_countries(returnclass ="sf") world_map_dataworld_map_financed_emissions <- world_map_data |>left_join(financed_emissions_2018, by =c("iso_a3"="iso3c"))
Code
world_map_financed_emissions %>%filter(iso_a3 !="ATA") %>%ggplot(aes(fill = financed_emission/1000)) +# this makes a map using the sf data. So easy, right?geom_sf() +theme(legend.position="bottom") +labs(title ="Figure 3: Emissions Financed by Sovereign Debt (billions of tons of CO2)",subtitle =str_wrap("Sovereign Debt finances the most emissions in China and the United States", width =70),fill =NULL,caption ="Sources: Climate Watch, OECD, IMF, World Bank | Latest Data: 2018 | Calculations by Ryan Showman" )
Code
financed_emissions_2018 <- financed_emissions_2018 %>%arrange(financed_emission)financed_emissions_2018$positive_or_negative <-ifelse(financed_emissions_2018$financed_emission <0, "negative", "positive") financed_emissions_2018 %>%filter(financed_emission >200) %>%ggplot(aes(x =fct_reorder(.f = country_name, .x = financed_emission), y = financed_emission, fill = region)) +geom_bar(stat="identity", width= .9) +labs(title ="Figure 4: Emissions Financed by Sovereign Debt by Country",subtitle =str_wrap("The United States, China, and Japan finance the most emissions from sovereign debt.", width =70),x ="Country",y ="Emissions Financed from Sovereign Debt (millions of tons of CO2)", fill ="Region",caption ="Sources: Climate Watch, OECD, IMF, World Bank | Latest Data: 2018 | Calculations by Ryan Showman" ) +theme_minimal() +coord_flip()
Code
financed_emissions_no_na %>%group_by(year) %>%summarize(mean_financed_emission =mean(financed_emission), mean_gdp_ppp =mean(GDP_PPP_dollars), mean_debt =mean(debt)) %>%ggplot(aes(x = mean_gdp_ppp/1000000, y = mean_financed_emission)) +geom_line() +labs(title ="Figure 5: Average PPP Adjusted GDP v. Average Emissions Financed by Sovereign Debt",subtitle =str_wrap("There is a positive relationship between emissions financed by sovereign debt and PPP adjusted GDP. ", width =70),x ="Average PPP Adjusted GDP (USD)",y ="Average Emissions Financed by Sovereign Debt", caption ="Sources: Climate Watch, OECD, IMF, World Bank | Latest Data: 2018 | Calculations by Ryan Showman" ) +scale_x_continuous(labels = scales::label_dollar(suffix ="M"), expand =c(0,0)) +theme_minimal()
Code
financed_emissions_no_na %>%group_by(year) %>%summarize(mean_financed_emission =mean(financed_emission), mean_gdp_ppp =mean(GDP_PPP_dollars), mean_debt =mean(debt)) %>%ggplot(aes(x = mean_debt/1000000, y = mean_financed_emission)) +geom_line() +labs(title ="Figure 6: Average Sovereign Debt v. Average Emissions Financed by Sovereign Debt",subtitle =str_wrap("There is a positive relationship between emissions financed by sovereign debt and sovereign debt.", width =70),x ="Average Sovereign Debt (USD)",y ="Average Emissions Financed by Sovereign Debt", caption ="Sources: Climate Watch, OECD, IMF, World Bank | Latest Data: 2018 | Calculations by Ryan Showman" ) +scale_x_continuous(labels = scales::label_dollar(suffix ="M"), expand =c(0,0)) +theme_minimal()
Code
theme_set(theme_bw())graph_financed_emissions <- financed_emissions_no_na graph_financed_emissions$"year"<-as.numeric(as.character(graph_financed_emissions$"year"))graphic <-ggplot(graph_financed_emissions, aes(x = GDP_PPP_dollars/1000000, y=financed_emission, size = debt/1000000, colour = region) ) +geom_point(show.legend =TRUE, alpha =0.7) +scale_color_viridis_d() +scale_size(range =c(2, 12)) +scale_x_continuous(labels = scales::label_dollar(suffix ="M"), expand =c(0,0))graphic +transition_time(year) +labs(title ="Year: {frame_time}",subtitle =str_wrap("Emissions Financed by Sovereign Debt v. PPP Adjusted GDP v. Debt over Time ", width =70), x ="Average PPP Adjusted GDP (USD)",y ="Average Emissions Financed by Sovereign Debt (millions of tons of CO2)",size ="Sovereign Debt (millions of USD)",colour ="Region",caption ="Emissions Financed by SOvereign Debt seems to have a positive relationship with both PPP ajdusted GDP and Sovereign Debt over the years.| Sources: Climate Watch, OECD, IMF, World Bank | Latest Data: 2018 | Calculations by Ryan Showman" )