── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor) # for clean_names() - makes variable names snake_caseimf_climate_dashboards_green_debt_url <-"https://opendata.arcgis.com/datasets/8e2772e0b65f4e33a80183ce9583d062_0.csv"green_debt <- imf_climate_dashboards_green_debt_url |>read_csv()
Rows: 355 Columns: 42
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (12): Country, ISO2, ISO3, Indicator, Unit, Source, CTS_Code, CTS_Name, ...
dbl (30): ObjectId, F1985, F1986, F1987, F1990, F1991, F1992, F1993, F1994, ...
ℹ 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.
green_debt
# A tibble: 355 × 42
ObjectId Country ISO2 ISO3 Indicator Unit Source CTS_Code CTS_Name
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 Argentina AR ARG Green Bo… Bill… Refin… ECFFI Green B…
2 2 Australia AU AUS Green Bo… Bill… Refin… ECFFI Green B…
3 3 Austria AT AUT Green Bo… Bill… Refin… ECFFI Green B…
4 4 Austria AT AUT Sovereig… Bill… Refin… ECFF Green B…
5 5 Bangladesh BD BGD Green Bo… Bill… Refin… ECFFI Green B…
6 6 Belarus, Rep. … BY BLR Green Bo… Bill… Refin… ECFFI Green B…
7 7 Belarus, Rep. … BY BLR Sovereig… Bill… Refin… ECFF Green B…
8 8 Belgium BE BEL Green Bo… Bill… Refin… ECFFI Green B…
9 9 Belgium BE BEL Sovereig… Bill… Refin… ECFF Green B…
10 10 Bermuda BM BMU Green Bo… Bill… Refin… ECFFI Green B…
# ℹ 345 more rows
# ℹ 33 more variables: CTS_Full_Descriptor <chr>, Type_of_Issuer <chr>,
# Use_of_Proceed <chr>, Principal_Currency <chr>, F1985 <dbl>, F1986 <dbl>,
# F1987 <dbl>, F1990 <dbl>, F1991 <dbl>, F1992 <dbl>, F1993 <dbl>,
# F1994 <dbl>, F1999 <dbl>, F2000 <dbl>, F2002 <dbl>, F2003 <dbl>,
# F2004 <dbl>, F2007 <dbl>, F2008 <dbl>, F2009 <dbl>, F2010 <dbl>,
# F2011 <dbl>, F2012 <dbl>, F2013 <dbl>, F2014 <dbl>, F2015 <dbl>, …
# we want to compare these two indicatorsindicators_we_want <-c("Green Bond Issuances by Country", "Sovereign Green Bond Issuances")green_debt_subset <- green_debt |># from the janitor package -- makes variables snake_case so they are easier to work withclean_names() |># filter for the vector of indicators we defined abovefilter(indicator %in% indicators_we_want) |># "f\\d{4}" is a regular expression (regex) that searches for all columns that are f + four digits.# Ask ChatGPT to explain this to you.select(country, iso3, indicator, matches("f\\d{4}")) green_debt_subset
# A tibble: 107 × 32
country iso3 indicator f1985 f1986 f1987 f1990 f1991 f1992 f1993 f1994 f1999
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Argent… ARG Green Bo… NA NA NA NA NA NA NA NA NA
2 Austra… AUS Green Bo… NA NA NA NA NA NA NA NA NA
3 Austria AUT Green Bo… NA NA NA NA NA NA NA NA NA
4 Austria AUT Sovereig… NA NA NA NA NA NA NA NA NA
5 Bangla… BGD Green Bo… NA NA NA NA NA NA NA NA NA
6 Belaru… BLR Green Bo… NA NA NA NA NA NA NA NA NA
7 Belaru… BLR Sovereig… NA NA NA NA NA NA NA NA NA
8 Belgium BEL Green Bo… NA NA NA NA NA NA NA NA NA
9 Belgium BEL Sovereig… NA NA NA NA NA NA NA NA NA
10 Bermuda BMU Green Bo… NA NA NA NA NA NA NA NA NA
# ℹ 97 more rows
# ℹ 20 more variables: f2000 <dbl>, f2002 <dbl>, f2003 <dbl>, f2004 <dbl>,
# f2007 <dbl>, f2008 <dbl>, f2009 <dbl>, f2010 <dbl>, f2011 <dbl>,
# f2012 <dbl>, f2013 <dbl>, f2014 <dbl>, f2015 <dbl>, f2016 <dbl>,
# f2017 <dbl>, f2018 <dbl>, f2019 <dbl>, f2020 <dbl>, f2021 <dbl>,
# f2022 <dbl>
# A tibble: 3,103 × 5
country iso3 indicator name value
<chr> <chr> <chr> <chr> <dbl>
1 Argentina ARG Green Bond Issuances by Country f1985 NA
2 Argentina ARG Green Bond Issuances by Country f1986 NA
3 Argentina ARG Green Bond Issuances by Country f1987 NA
4 Argentina ARG Green Bond Issuances by Country f1990 NA
5 Argentina ARG Green Bond Issuances by Country f1991 NA
6 Argentina ARG Green Bond Issuances by Country f1992 NA
7 Argentina ARG Green Bond Issuances by Country f1993 NA
8 Argentina ARG Green Bond Issuances by Country f1994 NA
9 Argentina ARG Green Bond Issuances by Country f1999 NA
10 Argentina ARG Green Bond Issuances by Country f2000 NA
# ℹ 3,093 more rows
green_bonds_tidy <- green_debt_subset |>pivot_longer(# select all columns with f + 4 numbers cols =matches("f\\d{4}"),# change from default ("names")names_to ="year",# same with the valuesvalues_to ="issuance_bn_usd",# readr::parse_number is a handy function that changes the character string # "f2222" into the number 2222. Very useful! names_transform = readr::parse_number,# green bonds are new-ish, we can drop all those NA values in the 80s and 90s for now.values_drop_na =TRUE )
3.5.0.1 Homework problem 1:
green_debt_subset
# A tibble: 107 × 32
country iso3 indicator f1985 f1986 f1987 f1990 f1991 f1992 f1993 f1994 f1999
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Argent… ARG Green Bo… NA NA NA NA NA NA NA NA NA
2 Austra… AUS Green Bo… NA NA NA NA NA NA NA NA NA
3 Austria AUT Green Bo… NA NA NA NA NA NA NA NA NA
4 Austria AUT Sovereig… NA NA NA NA NA NA NA NA NA
5 Bangla… BGD Green Bo… NA NA NA NA NA NA NA NA NA
6 Belaru… BLR Green Bo… NA NA NA NA NA NA NA NA NA
7 Belaru… BLR Sovereig… NA NA NA NA NA NA NA NA NA
8 Belgium BEL Green Bo… NA NA NA NA NA NA NA NA NA
9 Belgium BEL Sovereig… NA NA NA NA NA NA NA NA NA
10 Bermuda BMU Green Bo… NA NA NA NA NA NA NA NA NA
# ℹ 97 more rows
# ℹ 20 more variables: f2000 <dbl>, f2002 <dbl>, f2003 <dbl>, f2004 <dbl>,
# f2007 <dbl>, f2008 <dbl>, f2009 <dbl>, f2010 <dbl>, f2011 <dbl>,
# f2012 <dbl>, f2013 <dbl>, f2014 <dbl>, f2015 <dbl>, f2016 <dbl>,
# f2017 <dbl>, f2018 <dbl>, f2019 <dbl>, f2020 <dbl>, f2021 <dbl>,
# f2022 <dbl>
# Pivot data to long formatgreen_debt_long <- green_debt_subset |>pivot_longer(cols =matches("f\\d{4}"),# change from default ("names")names_to ="year" ,# same with the valuesvalues_to ="issuance_bn_usd" ,# readr::parse_number is a handy function that changes the character string # "f2222" into the number 2222. Very useful! names_transform = readr::parse_number ,# green bonds are new-ish, we can drop all those NA values in the 80s and 90s for now.values_drop_na =TRUE)green_debt_long
# A tibble: 465 × 5
country iso3 indicator year issuance_bn_usd
<chr> <chr> <chr> <dbl> <dbl>
1 Argentina ARG Green Bond Issuances by Country 2017 0.974
2 Argentina ARG Green Bond Issuances by Country 2020 0.0500
3 Argentina ARG Green Bond Issuances by Country 2021 0.916
4 Argentina ARG Green Bond Issuances by Country 2022 0.207
5 Australia AUS Green Bond Issuances by Country 2014 0.526
6 Australia AUS Green Bond Issuances by Country 2015 0.413
7 Australia AUS Green Bond Issuances by Country 2016 0.531
8 Australia AUS Green Bond Issuances by Country 2017 2.53
9 Australia AUS Green Bond Issuances by Country 2018 2.22
10 Australia AUS Green Bond Issuances by Country 2019 1.98
# ℹ 455 more rows
# Step 2: Add region to green_debt_longgreen_debt_long <- green_debt_long |>mutate(region =countrycode(iso3, "iso3c", "region"))# Step 3: Calculate cumulative issuance by regiongreen_debt_cumulative <- green_debt_long |>group_by(region) |>summarize(cumulative_issuance =sum(issuance_bn_usd, na.rm =TRUE))# Step 4: Create a ggplot visualizationregion_plot <-ggplot(green_debt_cumulative, aes(x =reorder(region, cumulative_issuance), y = cumulative_issuance)) +geom_col(fill ="forestgreen") +coord_flip() +theme_minimal() +scale_y_continuous(labels = scales::label_dollar(suffix =" bn"),expand =c(0,0)) +labs(title ="Cumulative Green Bond Issuance by Region",subtitle ="Europe and Central Asia are leading the pack",x ="Region",y ="Cumulative Issuance",caption ="Data Source: IMF Climate Finance Dashboard|Insight: NP Mnguni")region_plot
3.5.0.2 Homework problem 2:
# 1. Clean the variable namesgreen_debt_hw2 <- green_debt |>clean_names()# 2. Filter out "Not Applicable" observations from 'type_of_issuer'green_debt_filtered_hw2 <- green_debt_hw2 |>filter(type_of_issuer !="Not Applicable")# 3. Pivot the data from wide to long formatgreen_debt_long_hw2 <- green_debt_filtered_hw2 |>pivot_longer(cols =starts_with("f"), names_to ="year", values_to ="issuance",names_prefix ="f",names_transform =list(year = as.integer) ) |>drop_na(issuance) # This drops all NA values, ensuring only years with issuances are kept# 4. (Optional) Summarize data if needed - here we sum by type of issuer and yeargreen_debt_summary_hw2 <- green_debt_long_hw2 |>group_by(type_of_issuer, year) |>summarize(total_issuance =sum(issuance, na.rm =TRUE), .groups ='drop')# 5. Create a data visualization# For example, a line graph showing the trend of issuances over time for each type of issuerissuance_trend_plot <-ggplot(green_debt_summary_hw2, aes(x = year, y = total_issuance, color = type_of_issuer)) +geom_line() +theme_minimal() +labs(title ="Trend of Green Bond Issuances Over Time by Issuer Type",x ="Year",y ="Total Issuance",color ="Type of Issuer")# Print the plotissuance_trend_plot
# Summarize data by type_of_issuer and yeargreen_debt_summary_hw2 <- green_debt_long_hw2 |>group_by(type_of_issuer, year) |>summarize(total_issuance =sum(issuance, na.rm =TRUE), .groups ='drop') |>clean_names() # This will also convert 'type_of_issuer' values to snake_casegreen_debt_summary_hw2
# Now pivot widerissuer_factoids <- green_debt_summary_hw2 |>pivot_wider(names_from = type_of_issuer, values_from = total_issuance# No need for names_transform because clean_names() has already been used ) |>clean_names() issuer_factoids
# A tibble: 29 × 8
year banks international_organization local_and_state_government
<int> <dbl> <dbl> <dbl>
1 1985 0.03 NA NA
2 1986 0.3 NA NA
3 1987 0.0751 NA NA
4 1990 0 NA NA
5 1999 0.0482 NA NA
6 2000 0.0533 NA NA
7 2002 0.148 NA NA
8 2003 0.01 NA NA
9 2004 0.109 NA NA
10 2013 0.757 7.18 0.0475
# ℹ 19 more rows
# ℹ 4 more variables: nonfinancial_corporations <dbl>,
# other_financial_corporations <dbl>, sovereign <dbl>,
# state_owned_entities <dbl>
# Now you can use these new column names directly in R code# Let's say we want to calculate the multiple and percentage more for two types of issuers# Replace 'nonfinancial_corporations' and 'state_owned_entities' with actual snake_case names from your dataissuer_factoids <- issuer_factoids |>mutate(# Multiples: Banks issued X times more than Nonfinancial corporationsbanks_x_than_nonfinancial_corporations = banks / nonfinancial_corporations,# Percent more: Nonfinancial corporations issued X% more than Banksbanks_pct_more_than_nonfinancial_corporations = (banks/nonfinancial_corporations-1) *100 )# Replace 'nonfinancial_corporations' and 'banks' with actual cleaned column names from pivot_wider# Now you can create your factoids factoids <- issuer_factoids |>summarise(banks_x_times_nonfinancial =first(banks_x_than_nonfinancial_corporations),banks_pct_over_nonfinancial =first(banks_pct_more_than_nonfinancial_corporations ) )#factoidsprint(factoids)
#Banks issued" 1.2 times more than Nonfinancial corporations#Nonfinancial corporations issued 20% more than Banks
3.5.0.3 Homework problem 3: green bond proceeds
#3.5.0.3 Homework problem 3: # Pivot the data from wide to long format for use_of_proceed green_debt_long_proceed <- green_debt_hw2 |>filter(use_of_proceed !="Not Applicable") |>pivot_longer(cols =starts_with("f"),names_to ="year",values_to ="share",names_prefix ="f",names_transform =list(year = as.integer) ) green_debt_long_proceed
# A tibble: 4,002 × 15
object_id country iso2 iso3 indicator unit source cts_code cts_name
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
2 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
3 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
4 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
5 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
6 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
7 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
8 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
9 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
10 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
# ℹ 3,992 more rows
# ℹ 6 more variables: cts_full_descriptor <chr>, type_of_issuer <chr>,
# use_of_proceed <chr>, principal_currency <chr>, year <int>, share <dbl>
# A tibble: 69 × 82
object_id country iso2 iso3 indicator unit source cts_code cts_name
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 209 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
2 211 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
3 213 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
4 215 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
5 217 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
6 219 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
7 221 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
8 223 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
9 225 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
10 227 World <NA> WLD Cumulative Gree… Share Refin… ECFF Green B…
# ℹ 59 more rows
# ℹ 73 more variables: cts_full_descriptor <chr>, type_of_issuer <chr>,
# principal_currency <chr>, year <int>, access_to_essential_services <dbl>,
# acquiring_and_distribution_of_vaccine <dbl>, acquisition <dbl>,
# affordable_basic_infrastructure <dbl>, agriculture <dbl>,
# alternative_energy <dbl>, aquatic_biodiversity_conservation <dbl>,
# capital_expenditure <dbl>, capital_expenditure_financing_expenses <dbl>, …
# Assuming object_id is the first column and year is the last column FROM HERE green_debt_wide_long <- green_debt_wide |>pivot_longer(cols = access_to_essential_services:other, # Pivot all columns except 'object_id' and 'year'names_to ="use_of_proceeds",values_to ="share" ) |>drop_na(share)# Select the range of columns first, then pivot green_debt_wide_long <- green_debt_wide |>select(access_to_essential_services:other) |># Select only the columns you want to keeppivot_longer(cols =everything(), # Pivot all columns except 'year'names_to ="use_of_proceeds",values_to ="share" ) |>drop_na(use_of_proceeds, share) # Drop rows where 'use_of_proceeds' or 'share' is NA# View the resulting long dataframe green_debt_wide_long
# Calculate the total share for each category category_totals <- green_debt_wide_long |>group_by(use_of_proceeds) |>summarize(total_share =sum(share)) |>ungroup()# Define a cutoff point for the smallest categories to be grouped into 'Other' cutoff <-0.1# This is an arbitrary cutoff, adjust based on your data# Create a new category grouping the smallest ones into 'Other' green_debt_wide_long_s <- green_debt_wide_long |>left_join(category_totals, by ="use_of_proceeds") |>mutate(use_of_proceeds =if_else(total_share < cutoff, "other", use_of_proceeds)) |>group_by(use_of_proceeds) |>summarize(share =sum(share)) |>ungroup()# Now create the pie chart pie_chart <-ggplot(green_debt_wide_long_s, aes(x ="", y = share, fill = use_of_proceeds)) +geom_bar(stat ="identity", width =1) +coord_polar("y", start =0) +theme_void() +labs(title ="Green Bond Issuance by Use of Proceeds for 2022", fill ="Use of Proceeds") +scale_fill_viridis_d()# Display the pie chartprint (pie_chart)
# Procceeds are largely used for clean transport
3.5.0.3 Homework problem 3: green bond proceeds
# First, pivot the data from wide to long format green_debt_long_currency <- green_debt_hw2 |>pivot_longer(cols =starts_with("f"), names_to ="year", values_to ="share",names_prefix ="f",names_transform =list(year = as.integer) )# Filter rows where the unit is "Share" long_currency_filtered <- green_debt_long_currency |>filter(unit =="Share")# You don't need to filter share != "NA" as it will be NA, not the string "NA"# Also, you don't need to select again if you're not changing the columns green_debt_wide <- long_currency_filtered |>filter(principal_currency !="Not Applicable") # Calculate the total share for each currency category_totals <- green_debt_wide |>group_by(principal_currency) |>summarize(total_share =sum(as.numeric(share), na.rm =TRUE)) |>ungroup()# Define a cutoff point for the smallest categories to be grouped into 'Other' cutoff <-0.1# Adjust based on your data# Group the smallest currencies into 'Other' green_debt_wide_s <- green_debt_wide |>left_join(category_totals, by ="principal_currency") |>mutate(principal_currency =if_else(total_share < cutoff, "Other", principal_currency)) |>group_by(principal_currency) |>summarize(share =sum(as.numeric(share), na.rm =TRUE)) |>ungroup() green_debt_wide_s
# A tibble: 4 × 2
principal_currency share
<chr> <dbl>
1 Chinese Yuan 0.142
2 Euro 0.427
3 Other 0.176
4 US Dollar 0.254
# Calculate percentages for each principal_currency green_debt_wide_s <- green_debt_wide_s |>mutate(percentage = share /sum(share) *100)# Create the pie chart with percentages and bright colors pie_chart_currency <-ggplot(green_debt_wide_s, aes(x ="", y = share, fill = principal_currency)) +geom_bar(stat ="identity", width =1) +coord_polar("y", start =0) +theme_void() +labs(title ="Green Bond Issuance by Currency for 2022", fill ="Currency") +scale_fill_brewer(palette ="Set2") +# Use a bright color palette from RColorBrewergeom_text(aes(label =sprintf("%0.1f%%", percentage)), position =position_stack(vjust =0.5))# Display the pie chartprint(pie_chart_currency)
#Currency doesnt appear to be changing over time, its largely in Euros and Chinese Yuan.