HW W5

Prep work

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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) 
Warning: package 'janitor' was built under R version 4.2.3

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(countrycode)
Warning: package 'countrycode' was built under R version 4.2.3
library(ggplot2)
library(hrbrthemes)
Warning: package 'hrbrthemes' was built under R version 4.2.3
NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
      Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
      if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(viridis)
Warning: package 'viridis' was built under R version 4.2.3
Loading required package: viridisLite
Warning: package 'viridisLite' was built under R version 4.2.3

Problem 1

# install.packages("tidyverse")
# install.packages("janitor")
# install.packages("countrycode")

library(tidyverse)
library(janitor) 
library(countrycode)
library(ggplot2)

imf_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>, …
indicators_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 with
  clean_names() |> 
  # filter for the vector of indicators we defined above
  filter(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>
green_bonds_tidy <- green_debt_subset |> 
  pivot_longer(
    # select all coluns with f + 4 numbers 
    cols = matches("f\\d{4}"),
    
    # change from default ("names")
    names_to = "year",
    # same with the values
    values_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_bonds_tidy
# 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
#465 rows

green_bonds_tidy_w_region <- green_bonds_tidy |> 
  mutate(region = countrycode(country,"country.name" ,"region" ),
         .before =1) |>
  select(-iso3,-country)

green_bonds_tidy_w_region          
# A tibble: 465 × 4
   region                    indicator                      year issuance_bn_usd
   <chr>                     <chr>                         <dbl>           <dbl>
 1 Latin America & Caribbean Green Bond Issuances by Coun…  2017          0.974 
 2 Latin America & Caribbean Green Bond Issuances by Coun…  2020          0.0500
 3 Latin America & Caribbean Green Bond Issuances by Coun…  2021          0.916 
 4 Latin America & Caribbean Green Bond Issuances by Coun…  2022          0.207 
 5 East Asia & Pacific       Green Bond Issuances by Coun…  2014          0.526 
 6 East Asia & Pacific       Green Bond Issuances by Coun…  2015          0.413 
 7 East Asia & Pacific       Green Bond Issuances by Coun…  2016          0.531 
 8 East Asia & Pacific       Green Bond Issuances by Coun…  2017          2.53  
 9 East Asia & Pacific       Green Bond Issuances by Coun…  2018          2.22  
10 East Asia & Pacific       Green Bond Issuances by Coun…  2019          1.98  
# ℹ 455 more rows
View(green_bonds_tidy_w_region)
#465

region_yearly_sum <- green_bonds_tidy_w_region |>
  group_by(region, year,indicator) |>
  summarize(
    total_issuance_bn_usd = sum(issuance_bn_usd, na.rm=TRUE)
  ) |>
  ungroup()
`summarise()` has grouped output by 'region', 'year'. You can override using
the `.groups` argument.
region_yearly_sum
# A tibble: 101 × 4
   region               year indicator                     total_issuance_bn_usd
   <chr>               <dbl> <chr>                                         <dbl>
 1 East Asia & Pacific  2012 Green Bond Issuances by Coun…                0.0311
 2 East Asia & Pacific  2013 Green Bond Issuances by Coun…                0.831 
 3 East Asia & Pacific  2014 Green Bond Issuances by Coun…                1.08  
 4 East Asia & Pacific  2015 Green Bond Issuances by Coun…                2.62  
 5 East Asia & Pacific  2016 Green Bond Issuances by Coun…               32.4   
 6 East Asia & Pacific  2017 Green Bond Issuances by Coun…               36.6   
 7 East Asia & Pacific  2017 Sovereign Green Bond Issuanc…                0.0445
 8 East Asia & Pacific  2018 Green Bond Issuances by Coun…               46.4   
 9 East Asia & Pacific  2019 Green Bond Issuances by Coun…               71.1   
10 East Asia & Pacific  2019 Sovereign Green Bond Issuanc…                3     
# ℹ 91 more rows
#82rows

region_sum_cumulative <- region_yearly_sum |> 
  arrange(region, year) |> 
  group_by(region, indicator) |> 
  mutate(cumulative_bn_usd = cumsum(total_issuance_bn_usd)) |> 
  ungroup()

region_sum_cumulative
# A tibble: 101 × 5
   region               year indicator   total_issuance_bn_usd cumulative_bn_usd
   <chr>               <dbl> <chr>                       <dbl>             <dbl>
 1 East Asia & Pacific  2012 Green Bond…                0.0311            0.0311
 2 East Asia & Pacific  2013 Green Bond…                0.831             0.863 
 3 East Asia & Pacific  2014 Green Bond…                1.08              1.94  
 4 East Asia & Pacific  2015 Green Bond…                2.62              4.56  
 5 East Asia & Pacific  2016 Green Bond…               32.4              37.0   
 6 East Asia & Pacific  2017 Green Bond…               36.6              73.5   
 7 East Asia & Pacific  2017 Sovereign …                0.0445            0.0445
 8 East Asia & Pacific  2018 Green Bond…               46.4             120.    
 9 East Asia & Pacific  2019 Green Bond…               71.1             191.    
10 East Asia & Pacific  2019 Sovereign …                3                 3.04  
# ℹ 91 more rows
#101

region_cumulative <- region_sum_cumulative |> 
  filter(indicator == "Green Bond Issuances by Country") |> 
  group_by(region) |> 
  slice_max(order_by = year) |> 
  arrange(desc(cumulative_bn_usd)) |> 
  select(region, cumulative_bn_usd) |> 
  ungroup()

region_cumulative
# A tibble: 7 × 2
  region                     cumulative_bn_usd
  <chr>                                  <dbl>
1 Europe & Central Asia                1161.  
2 East Asia & Pacific                   567.  
3 North America                         236.  
4 Latin America & Caribbean              92.1 
5 Sub-Saharan Africa                     16.1 
6 South Asia                             15.0 
7 Middle East & North Africa              8.85
# plot a graph
chart <- region_cumulative |> 
    ggplot(aes(x = cumulative_bn_usd, 
             # order countries by cumulative issuance
             y = fct_reorder(.f = region, .x = cumulative_bn_usd)
              )
          ) +
  geom_col(fill = "forestgreen")+
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Regions",
       subtitle = "The Europeans sure do borrow, the Saudis are just sitting on cash",
       x = "Cumulative Issuance (USD)",
       y = "",
       caption = "Data: IMF Climate Change Dashboard | Insight: Dandan")

chart

Problem 2

# Use the full green_debt dataset
# Use clean_names() to make the variable names snake_case
# Filter out observations where type_of_issuer is “Not Applicable”
# Use the tools taught in this chapter to provide a compelling data visualization and some repeatable factoids that provide actionable insights about green bond issuers.

colnames(green_debt)
 [1] "ObjectId"            "Country"             "ISO2"               
 [4] "ISO3"                "Indicator"           "Unit"               
 [7] "Source"              "CTS_Code"            "CTS_Name"           
[10] "CTS_Full_Descriptor" "Type_of_Issuer"      "Use_of_Proceed"     
[13] "Principal_Currency"  "F1985"               "F1986"              
[16] "F1987"               "F1990"               "F1991"              
[19] "F1992"               "F1993"               "F1994"              
[22] "F1999"               "F2000"               "F2002"              
[25] "F2003"               "F2004"               "F2007"              
[28] "F2008"               "F2009"               "F2010"              
[31] "F2011"               "F2012"               "F2013"              
[34] "F2014"               "F2015"               "F2016"              
[37] "F2017"               "F2018"               "F2019"              
[40] "F2020"               "F2021"               "F2022"              
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>, …
green_debt |>
  distinct(Type_of_Issuer)
# A tibble: 8 × 1
  Type_of_Issuer              
  <chr>                       
1 Not Applicable              
2 Banks                       
3 International Organization  
4 Local and state Government  
5 Nonfinancial corporations   
6 Other financial corporations
7 Sovereign                   
8 State owned entities        
#355
green_debt_w_issuer <- green_debt |> 
  clean_names() |> 
  filter(type_of_issuer != "Not Applicable") |>
  select(type_of_issuer, matches("f\\d{4}"))

green_debt_w_issuer
# A tibble: 7 × 30
  type_of_issuer         f1985 f1986   f1987 f1990   f1991  f1992   f1993  f1994
  <chr>                  <dbl> <dbl>   <dbl> <dbl>   <dbl>  <dbl>   <dbl>  <dbl>
1 Banks                  0.03    0.3  0.0751     0 NA      NA     NA      NA    
2 International Organi… NA      NA   NA         NA NA      NA     NA      NA    
3 Local and state Gove… NA      NA   NA         NA  0.0292  0.144  0.0175  0.110
4 Nonfinancial corpora…  0.025  NA   NA         NA NA      NA      0.168  NA    
5 Other financial corp… NA      NA   NA         NA NA      NA     NA      NA    
6 Sovereign             NA      NA   NA         NA NA      NA     NA      NA    
7 State owned entities  NA      NA   NA         NA NA      NA     NA      NA    
# ℹ 21 more variables: 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>,
#   f2016 <dbl>, f2017 <dbl>, f2018 <dbl>, f2019 <dbl>, f2020 <dbl>,
#   f2021 <dbl>, f2022 <dbl>
#7

green_debt_w_issuer_tidy <- green_debt_w_issuer |> 
  pivot_longer(
    # select all coluns with f + 4 numbers 
    cols = matches("f\\d{4}"),
    
    # change from default ("names")
    names_to = "year",
    # same with the values
    values_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_w_issuer_tidy
# A tibble: 98 × 3
   type_of_issuer  year issuance_bn_usd
   <chr>          <dbl>           <dbl>
 1 Banks           1985          0.03  
 2 Banks           1986          0.3   
 3 Banks           1987          0.0751
 4 Banks           1990          0     
 5 Banks           1999          0.0482
 6 Banks           2000          0.0533
 7 Banks           2002          0.148 
 8 Banks           2003          0.01  
 9 Banks           2004          0.109 
10 Banks           2013          0.757 
# ℹ 88 more rows
# 98

library()

green_debt_w_issuer_tidy |>
  ggplot(aes(fill=type_of_issuer, y=issuance_bn_usd, x=year)) + 
  geom_bar(position="stack", stat="identity") +
  scale_fill_viridis(discrete = T) +
  theme_ipsum() +
  labs(title = "Green bond growth largely driven by nonfinancial corporations",
       subtitle = "2021 marks historic issuance volume",
       x = "Year",
       y = "Billions USD",
       caption = "Data: IMF Climate Change Dashboard | Insight: Dandan")
Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database

Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database

Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database

Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Problem 3

# Repeat the process from problem 2 for use_of_proceed and for principal_currency
# What are green bond proceeds used for?
# What do we know about the currency of issuance? Is that changing over time?
colnames(green_debt)
 [1] "ObjectId"            "Country"             "ISO2"               
 [4] "ISO3"                "Indicator"           "Unit"               
 [7] "Source"              "CTS_Code"            "CTS_Name"           
[10] "CTS_Full_Descriptor" "Type_of_Issuer"      "Use_of_Proceed"     
[13] "Principal_Currency"  "F1985"               "F1986"              
[16] "F1987"               "F1990"               "F1991"              
[19] "F1992"               "F1993"               "F1994"              
[22] "F1999"               "F2000"               "F2002"              
[25] "F2003"               "F2004"               "F2007"              
[28] "F2008"               "F2009"               "F2010"              
[31] "F2011"               "F2012"               "F2013"              
[34] "F2014"               "F2015"               "F2016"              
[37] "F2017"               "F2018"               "F2019"              
[40] "F2020"               "F2021"               "F2022"              
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>, …
# 355
green_debt |>
  distinct(Use_of_Proceed)
# A tibble: 70 × 1
   Use_of_Proceed                        
   <chr>                                 
 1 Not Applicable                        
 2 Access to Essential Services          
 3 Acquiring and distribution of vaccine 
 4 Acquisition                           
 5 Affordable Basic Infrastructure       
 6 Agriculture                           
 7 Alternative Energy                    
 8 Aquatic Biodiversity Conservation     
 9 Capital expenditure                   
10 Capital expenditure/Financing expenses
# ℹ 60 more rows
#70

colnames(green_debt)
 [1] "ObjectId"            "Country"             "ISO2"               
 [4] "ISO3"                "Indicator"           "Unit"               
 [7] "Source"              "CTS_Code"            "CTS_Name"           
[10] "CTS_Full_Descriptor" "Type_of_Issuer"      "Use_of_Proceed"     
[13] "Principal_Currency"  "F1985"               "F1986"              
[16] "F1987"               "F1990"               "F1991"              
[19] "F1992"               "F1993"               "F1994"              
[22] "F1999"               "F2000"               "F2002"              
[25] "F2003"               "F2004"               "F2007"              
[28] "F2008"               "F2009"               "F2010"              
[31] "F2011"               "F2012"               "F2013"              
[34] "F2014"               "F2015"               "F2016"              
[37] "F2017"               "F2018"               "F2019"              
[40] "F2020"               "F2021"               "F2022"              
green_debt_w_proceed <- green_debt |> 
  clean_names() |> 
  filter(use_of_proceed != "Not Applicable") |>
  filter(unit == "Billion US Dollars") |>
  select(use_of_proceed, matches("f\\d{4}"))

green_debt_w_proceed
# A tibble: 69 × 30
   use_of_proceed    f1985 f1986 f1987 f1990 f1991 f1992 f1993 f1994 f1999 f2000
   <chr>             <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 Access to Essent…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 2 Acquiring and di…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 3 Acquisition          NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 4 Affordable Basic…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 5 Agriculture          NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 6 Alternative Ener…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 7 Aquatic Biodiver…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 8 Capital expendit…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 9 Capital expendit…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
10 Carbon reduction…    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
# ℹ 59 more rows
# ℹ 19 more variables: 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>
#69

green_debt_w_proceed_tidy <- green_debt_w_proceed |> 
  pivot_longer(
    # select all coluns with f + 4 numbers 
    cols = matches("f\\d{4}"),
    # change from default ("names")
    names_to = "year",
    # same with the values
    values_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_w_proceed_tidy
# A tibble: 69 × 3
   use_of_proceed                                           year issuance_bn_usd
   <chr>                                                   <dbl>           <dbl>
 1 Access to Essential Services                             2022         12.8   
 2 Acquiring and distribution of vaccine                    2022          0.4   
 3 Acquisition                                              2022          7.39  
 4 Affordable Basic Infrastructure                          2022          0.0428
 5 Agriculture                                              2022          0.503 
 6 Alternative Energy                                       2022         25.7   
 7 Aquatic Biodiversity Conservation                        2022         75.7   
 8 Capital expenditure                                      2022          1.5   
 9 Capital expenditure/Financing expenses                   2022          1.81  
10 Carbon reduction through reforestation and avoided def…  2022          4.98  
# ℹ 59 more rows
# 69

green_debt_w_proceed_tidy_top20 <- green_debt_w_proceed_tidy |>
  slice_max(issuance_bn_usd, n=20) |>
  mutate(perc = `issuance_bn_usd` / sum(`issuance_bn_usd`)) |> 
  arrange(perc) |>
  mutate(labels = scales::percent(perc))

green_debt_w_proceed_tidy_top20
# A tibble: 20 × 5
   use_of_proceed                            year issuance_bn_usd    perc labels
   <chr>                                    <dbl>           <dbl>   <dbl> <chr> 
 1 Sustainable Development Projects          2022            2.87 0.00128 0.127…
 2 China Urban Construction                  2022            3.34 0.00149 0.148…
 3 Redeem Existing Bonds or Securities       2022            3.53 0.00157 0.157…
 4 Environmental Protection Projects         2022            4.17 0.00185 0.185…
 5 Carbon reduction through reforestation …  2022            4.98 0.00221 0.221…
 6 Other Education                           2022            7.39 0.00328 0.328…
 7 Acquisition                               2022            7.39 0.00329 0.328…
 8 General Purpose                           2022            9.45 0.00420 0.420…
 9 Pollution Prevention & Control            2022           11.1  0.00495 0.495…
10 Access to Essential Services              2022           12.8  0.00570 0.570…
11 Sustainable Water or Wastewater managem…  2022           17.7  0.00785 0.785…
12 Alternative Energy                        2022           25.7  0.0114  1.142…
13 Circular Economy Adapted/Eco-efficient …  2022           33.6  0.0149  1.491…
14 Aquatic Biodiversity Conservation         2022           75.7  0.0336  3.364…
15 Renewable Energy Projects                 2022          108.   0.0482  4.817…
16 Green Construction/Buildings              2022          127.   0.0566  5.656…
17 Eligible Green Projects                   2022          204.   0.0907  9.068…
18 Climate Change Adaptation                 2022          277.   0.123   12.31…
19 Energy Efficiency                         2022          480.   0.213   21.33…
20 Clean Transport                           2022          833.   0.370   37.04…
green_debt_w_proceed_tidy_top20|>
  ggplot(aes(x = "", y = perc, fill = use_of_proceed)) +
  geom_col() +
  coord_polar(theta = "y") +
  scale_fill_viridis_d() +
  theme_void() +
  guides(fill = guide_legend(title = "Use of proceeds"))+
  labs(title = "Top 20 green bond use of proceeds",
       subtitle = "Creative and diverse usage of green finance",
       # x = "Year",
       # y = "Billions USD",
       caption = "Data: IMF Climate Change Dashboard | Insight: Dandan")

green_debt_pc <- green_debt |>
  clean_names() |>
  filter(principal_currency != "Not Applicable") |>
  pivot_longer(
    cols = matches("f\\d{4}"),
    names_to = "Year", 
    values_to = "Issuance_bn_usd",
    values_drop_na = TRUE
  ) |>
  select(principal_currency, Year, Issuance_bn_usd) |>
  filter(!grepl("e", Issuance_bn_usd))

unique(green_debt_pc$principal_currency)
 [1] "Argentinian Peso"                         
 [2] "Argentinian Unidades de Valor Adquisitivo"
 [3] "Australian Dollar"                        
 [4] "Bangladeshi Taka"                         
 [5] "Brazilian Real"                           
 [6] "British Pound"                            
 [7] "Canadian Dollar"                          
 [8] "Chilean Peso"                             
 [9] "Chilean Unidad de Fomento"                
[10] "Chinese Yuan"                             
[11] "Colombian Peso"                           
[12] "Czech Koruna"                             
[13] "Danish Krone"                             
[14] "Deutsche Mark"                            
[15] "Euro"                                     
[16] "European Currency Unit"                   
[17] "Fijian Dollar"                            
[18] "Hong Kong Dollar"                         
[19] "Hungarian Forint"                         
[20] "Icelandic Krona"                          
[21] "Indian Rupee"                             
[22] "Indonesian Rupiah"                        
[23] "Japanese Yen"                             
[24] "Kazakhstani Tenge"                        
[25] "Macanese Pataca"                          
[26] "Malaysian Ringgit"                        
[27] "Mexican Peso"                             
[28] "Mexican Unidad de Inversion"              
[29] "Moroccan Dirham"                          
[30] "Namibian Dollar"                          
[31] "New Zealand Dollar"                       
[32] "Nigerian Naira"                           
[33] "Norwegian Krone"                          
[34] "Peruvian Sol"                             
[35] "Philippine Peso"                          
[36] "Polish Zloty"                             
[37] "Romanian Leu"                             
[38] "Russian Ruble"                            
[39] "Singapore Dollar"                         
[40] "South African Rand"                       
[41] "South Korean Won"                         
[42] "Sri Lankan Rupee"                         
[43] "Swedish Krona"                            
[44] "Swiss Franc"                              
[45] "Taiwanese Dollar"                         
[46] "Thai Baht"                                
[47] "Turkish Lira"                             
[48] "Ukrainian Hryvnia"                        
[49] "US Dollar"                                
[50] "Vietnamese Dong"                          
currency_counts <- green_debt_pc %>%
  group_by(principal_currency) %>%
  summarize(total_issuance = sum(Issuance_bn_usd)) %>%
  arrange(desc(total_issuance))

currency_chart <- ggplot(head(currency_counts, 15), aes(x = total_issuance, y = reorder(principal_currency, total_issuance))) +
  geom_col(fill = "grey") +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"), expand = c(0, 0)) +
  labs(x = "Total Issuance in Billion USD", y = "Currency", 
       title = "Green Bond Issuance by Currencies") +
  theme_minimal() 

currency_chart