Homework - Data Tidying

Author

Lexi Lei

Problem 1

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
library(countrycode)

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.
indicators_we_want <- c("Green Bond Issuances by Country", "Sovereign Green Bond Issuances")

green_debt_subset <- green_debt |> 
  clean_names() |> 
  filter(indicator %in% indicators_we_want) |> 
  select(country, iso3, indicator, matches("f\\d{4}")) 

green_debt_subset$region <- countrycode(green_debt_subset$iso3, "iso3c", "region")

green_bonds_tidy <- green_debt_subset |>
  pivot_longer(
    cols = matches("f\\d{4}"),
    names_to = "year",
    values_to = "issuance_bn_usd",
    names_transform = readr::parse_number,
    values_drop_na = TRUE
  )

green_bonds_cumulative <- green_bonds_tidy |>
  select(-iso3) |> 
  arrange(region) |> 
  group_by(region) |> 
  mutate(cumulative_bn_usd = cumsum(issuance_bn_usd)) |>
  slice_max(order_by = cumulative_bn_usd) |> 
  arrange(cumulative_bn_usd|> desc()) |> 
  select(region, cumulative_bn_usd) |> 
  ungroup()

green_bonds_cumulative |>
  ggplot(aes(x = cumulative_bn_usd, 
             y = fct_reorder(.f = region, .x = cumulative_bn_usd)
             )) +
  geom_col(fill = "#4682B4") +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Cumulative Issuance of Green Bonds by Region",
       subtitle = "Europe Takes Lead in Green Bond Issuance",
       x = "Cumulative Issuance (USD)",
       y = "",
       caption = "Data: IMF Climate Change Dashboard | Insight: Lexi"
  )

Problem 2

green_bond_issuer <- green_debt |> 
  clean_names() |> 
  filter(type_of_issuer != "Not Applicable")

ggplot(green_bond_issuer) +
  aes(x = fct_reorder(.f = type_of_issuer, .x = f2022), 
      weight = f2022) +
  geom_bar(fill = "#4682B4") +
  scale_y_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(x = "",
       y = "Issuance (USD)",
       title = "Green Bond Issurance in 2022",
       subtitle = "Nonfinancial Corporations and Banks Are the Largest Issuers",
       caption = "Data: IMF Climate Change Dashboard | Insight: Lexi"
      ) +
  theme_minimal() +
  coord_flip()

Problem 3

Use of Proceed

green_bond_proceed <- green_debt |> 
  clean_names() |> 
  filter(use_of_proceed != "Not Applicable")

top10_green_bond_proceed <- green_bond_proceed |>
  arrange(f2022 |> desc()) |> 
  slice_head(n = 10) |> 
  select(use_of_proceed, f2022) |> 
  ungroup()

ggplot(top10_green_bond_proceed) +
  aes(x = fct_reorder(.f = use_of_proceed, .x = f2022), 
      weight = f2022) +
  geom_bar(fill = "#4682B4") +
  scale_y_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(x = "",
       y = "",
       title = "Top 10 Use of Proceed of Green Bonds in 2022",
       subtitle = "Clean Transport Is the Largest Target of Green Bonds",
       caption = "Data: IMF Climate Change Dashboard | Insight: Lexi"
      ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip()

Principle Currency

green_bond_currency <- green_debt |> 
  clean_names() |> 
  filter(principal_currency != "Not Applicable")

top10_green_bond_currency <- green_bond_currency |>
  arrange(f2022 |> desc()) |> 
  slice_head(n = 10) |> 
  select(principal_currency, f2022) |> 
  ungroup()

ggplot(top10_green_bond_currency) +
  aes(x = fct_reorder(.f = principal_currency, .x = f2022), 
      weight = f2022) +
  geom_bar(fill = "#4682B4") +
  scale_y_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(x = "",
       y = "",
       title = "Top 10 Principal Currency of Green Bonds in 2022",
       subtitle = "EU, US, and China Are Taking Lead",
       caption = "Data: IMF Climate Change Dashboard | Insight: Lexi"
      ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip()