Data Tidying

Author

Anran Fang

Preparation Work

library(tidyverse)
── 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.4.4     ✔ 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)

Attaching package: 'janitor'

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

    chisq.test, fisher.test
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>, …

Problem 1:

  • Use the countrycode package to add the region as a variable to green_debt_subset.

  • Calculate the cumulative issuance of green bonds by region.

  • Use ggplot2 to make a data visualization comparing regional cumulative issuance.

  • Make your plot look professionally presentable, as we did above.

library(countrycode)

green_debt$region <- countrycode(green_debt$Country, 'country.name', 'region')
Warning: Some values were not matched unambiguously: World
cumulative_issuance_by_region <- green_debt %>%
  filter(!is.na(region)) %>%
  group_by(region) %>%
  summarize(cumulative_issuance = sum(across(starts_with('F')), na.rm = TRUE))

ggplot(cumulative_issuance_by_region, aes(x = region, y = cumulative_issuance)) +
  geom_bar(stat = "identity", fill = "forestgreen") +
  theme_minimal() +
  labs(title = "Cumulative Issuance of Green Bonds by Region", x = "Region", y = "Cumulative Issuance (in Billion USD)")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1), plot.title = element_text(hjust = 0.5))

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.

green_debt_subset <- green_debt %>%
  clean_names() %>%
  filter(type_of_issuer != "Not Applicable") %>%
  group_by(type_of_issuer) %>%
  summarize(count = n(), .groups = 'drop') %>%
  mutate(percentage = count / sum(count) * 100) # Ensure this is part of the pipeline

ggplot(green_debt_subset, aes(x = "", y = percentage, fill = type_of_issuer)) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0) +
  theme_minimal() +
  labs(title = "Distribution of Green Bonds Issued by Issuer Type", x = NULL, y = NULL) +
  theme(axis.text.x = element_blank(),
        axis.ticks = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        plot.title = element_text(hjust = 0.5)) +
  guides(fill = guide_legend(title = "Type of Issuer")) +
  geom_text(aes(label = paste0(round(percentage, 1), "%")), position = position_stack(vjust = 0.5))

Through this chart, we come to realize that among the known issuers of green bonds, the issuance is an average division between banks, international organization, local and state government, non-financial corporations, other financial corporations, sovereign, and state owned entities. However, considering the small sample size of this pie chart comparing to the raw data, we conclude that majority of green bond lack fundamental data of issuer.

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?

green_debt_subset_2 <- green_debt %>%
  clean_names() %>%
  filter(use_of_proceed != "Not Applicable")

count_of_use <- green_debt_subset_2 %>%
  group_by(use_of_proceed) %>%
  summarize(count = n(), .groups = 'drop')%>%
  mutate(percentage = count / sum(count) * 100)

ggplot(count_of_use, aes(x = "", y = count, fill = use_of_proceed)) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0) +
  theme_minimal() +
  labs(title = "Distribution of Green Bonds by Use of Proceed", x = NULL, y = NULL) +
  theme(axis.text.x = element_blank(),
        axis.ticks = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        plot.title = element_text(hjust = 0.5),
        legend.text = element_text(size = 1),  
        legend.title = element_text(size = 1)) + 
  guides(fill = guide_legend(title = "Type of Use Proceed"))+
  geom_text(aes(label = paste0(round(percentage, 1), "%")), position = position_stack(vjust = 0), size = 3) 

ggplot(count_of_use, aes(x = "", y = count)) +
  geom_boxplot() +
  theme_minimal() +
  labs(title = "Distribution of Amount Issued by Use of Proceed",
       x = "Use of Proceed",
       y = "Amount Issued") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1), plot.title = element_text(hjust = 0.5))

The pie chart’s label section displays all uses of these green bonds proceed, where each color label represents one use of proceed. Since there are too many uses of proceed in the data, the pie chart seems to show an even distribution between all uses, at roughly 1.4% weight for each use.

So we create a second graph, in which the box plot (though the box is compressed into a line) confirms the pattern displayed in pie chart that these green bonds divide evenly in their proceed uses, without one specific use that takes up significant weight.

green_debt_subset_3 <- green_debt %>%
  clean_names() %>%
  filter(principal_currency != "Not Applicable")

principal_currency_of_use <- green_debt_subset_3 %>%
  group_by(principal_currency) %>%
  summarize(count = n(), .groups = 'drop')%>%
  mutate(percentage = count / sum(count) * 100)

ggplot(principal_currency_of_use, aes(x = "", y = count, fill = principal_currency)) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0) +
  theme_minimal() +
  labs(title = "Distribution of Green Bonds by Principal Currency", x = NULL, y = NULL) +
  theme(axis.text.x = element_blank(),
        axis.ticks = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        plot.title = element_text(hjust = 0.5),
        legend.text = element_text(size = 1),  
        legend.title = element_text(size = 1)) +
  guides(fill = guide_legend(title = "Type of Principal Currency")) +
  geom_text(aes(label = paste0(round(percentage, 1), "%")), position = position_stack(vjust = 0), size = 3) 

The pie chart’s label section displays all principal currencies of these green bonds, each taken roughly 2% weight, still without a currency with significant weight.

green_debt_subset_4 <- green_debt %>%
  clean_names() %>%
  filter(principal_currency != "Not Applicable")%>%
  select(principal_currency, f1985:f2022)
green_debt_subset_4
# A tibble: 100 × 30
   principal_currency      f1985 f1986 f1987 f1990 f1991 f1992 f1993 f1994 f1999
   <chr>                   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 Argentinian Peso           NA    NA    NA    NA    NA    NA    NA    NA    NA
 2 Argentinian Peso           NA    NA    NA    NA    NA    NA    NA    NA    NA
 3 Argentinian Unidades d…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 4 Argentinian Unidades d…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 5 Australian Dollar          NA    NA    NA    NA    NA    NA    NA    NA    NA
 6 Australian Dollar          NA    NA    NA    NA    NA    NA    NA    NA    NA
 7 Bangladeshi Taka           NA    NA    NA    NA    NA    NA    NA    NA    NA
 8 Bangladeshi Taka           NA    NA    NA    NA    NA    NA    NA    NA    NA
 9 Brazilian Real             NA    NA    NA    NA    NA    NA    NA    NA    NA
10 Brazilian Real             NA    NA    NA    NA    NA    NA    NA    NA    NA
# ℹ 90 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>

From the table, we can tell that before 2022, principal currency data is missing for all green bonds, therefore it’s hard for us to analyze the changes in the currency of issuance over time.