#install our packages
install.packages("tidyverse") 
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("tidyquant")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("lubridate")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("plotly")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("esquisse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("here")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("janitor")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("ggthemes")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("ggrepel")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("gt")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("countrycode")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("wbstats")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
#load our packages
library(tidyverse) 
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## 
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## Attaching package: 'xts'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
## 
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(lubridate)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(esquisse)
library(here)
## here() starts at /cloud/project
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(ggthemes)
library(ggrepel)
library(gt)
library(countrycode)
library(wbstats)
options(scipen=10) # forces regular notation vs scientific notation (ie5)
#Load the IMF/WB country groups, and load the full emissions dataset
folder_path <- partial(here, "00_data", "sov_debt_paris_alignment")

imf_wb_country_groups <- folder_path("imf_wb_country_groups.rds") %>%
  read_rds()

emissions_dataset_full <- folder_path("emissions_dataset_full.rds") %>%
  read_rds()

emissions_dataset <- folder_path("emissions_dataset.rds") %>%
  read_rds()
#Calculate interesting summary statistics for at least one group 
emissions_dataset_full%>%
#I want to group by years
  group_by(year)%>%
#I want to see the average, minimum, maximum & mean value of the gdp ues current prices in each year
  summarise(average_gdp_usd_current_prices=mean(gdp_usd_current_prices,na.rm = TRUE),
            max_gdp_usd_current_prices=max(gdp_usd_current_prices,na.rm = TRUE),
            min_gdp_usd_current_prices=min(gdp_usd_current_prices,na.rm = TRUE),
            median_gdp_usd_current_prices=median(gdp_usd_current_prices,na.rm = TRUE)
  )
## # A tibble: 30 × 5
##     year average_gdp_usd_current_prices max_gdp_usd_current_pr…¹ min_g…² media…³
##    <dbl>                          <dbl>                    <dbl>   <dbl>   <dbl>
##  1  1990                           149.                    5963.   0.043    7.45
##  2  1991                           154.                    6158.   0.047    6.97
##  3  1992                           150.                    6520.   0.048    7.16
##  4  1993                           152.                    6859.   0.047    7.25
##  5  1994                           162.                    7287.   0.055    8.00
##  6  1995                           175.                    7640.   0.056    8.47
##  7  1996                           179.                    8073.   0.067    8.38
##  8  1997                           177.                    8578.   0.067    9.75
##  9  1998                           175.                    9063.   0.066    9.76
## 10  1999                           181.                    9631.   0.069   10.6 
## # … with 20 more rows, and abbreviated variable names
## #   ¹​max_gdp_usd_current_prices, ²​min_gdp_usd_current_prices,
## #   ³​median_gdp_usd_current_prices

Three professional charts.

Charts1: Top 8 emission countries by cumulative CO2 per capita in 2018

emissions_dataset %>%
  # filter the year in 2018
  filter(year == 2018) %>%
  # get the top 8 by cumulative Co2 per capita 
  slice_max(order_by = cumulative_co2_per_capita, n = 8) %>%
  # fct_reorder will sort x by cumulative Co2 per capita
  ggplot(aes(x = fct_reorder(.f = country_name,.x = cumulative_co2_per_capita, 
                             .desc = TRUE), 
              # fill will change the color by whether a country is EM or DM
             y = cumulative_co2_per_capita, fill = em_dm)) +
  # just get used to the `stat = identity` thing for bar charts, default is `count`
  geom_bar(stat = "identity") +
  geom_text(aes(label = format(cumulative_co2_per_capita, digits = 7, big.mark = ",")), vjust = -0.2) + 
  # expand the y limits so the text doesn't get cut off
  scale_y_continuous(limits = c(0, 1500), breaks = scales::pretty_breaks(n = 10)) +
  # change the color
  scale_fill_manual(values = c("Developed Markets" = "006090", "Emerging Markets" = "#007090")) +
  # a minimal theme with mostly white space
  theme_minimal() +
  labs(title = "Top 8 Emission Countries in 2018",
       subtitle = "based on cumulative CO2 per capita",
       x = "Country", 
       y = "Cumulative CO2 per capita (tons)",
       fill = "", # this gets rid of the `em_dm` tag on top of the legend,
       caption = "Data from WB/IMF, Analysis by Yulin"
       ) +
  # moves the legend from the side (default) to the bottom.
  theme(legend.position = "bottom") 

Chart2: Top8 emission countries by cumulative CO2 in 2018

emissions_dataset %>%
  # filter the year in 2018
  filter(year == 2018) %>%
  # get the top 8 by cumulative CO2
  slice_max(order_by = cumulative_co2, n = 8) %>%
  # fct_reorder will sort x by cumulative Co2
  ggplot(aes(x = fct_reorder(.f = country_name,.x = cumulative_co2,
                             .desc = TRUE), 
              # fill will change the color by whether a country is EM or DM
             y = cumulative_co2, fill = em_dm)) +
  # just get used to the `stat = identity` thing for bar charts, default is `count`
  geom_bar(stat = "identity") +
  geom_text(aes(label = format(cumulative_co2, digits = 7, big.mark = ",")), vjust = -0.2) + 
  # expand the y limits so the text doesn't get cut off
  scale_y_continuous(limits = c(0,450000), breaks = scales::pretty_breaks(n = 10)) +
  # change the color
  scale_fill_manual(values = c("Developed Markets" = "007020", "Emerging Markets" = "#005020")) +
  # a minimal theme with mostly white space
  theme_minimal() +
  labs(title = "Top 8 Emission Countries in 2018",
       subtitle = "based on cumulative CO2",
       x = "Country", 
       y = "Cumulative CO2 (tons)",
       fill = "", # this gets rid of the `em_dm` tag on top of the legend,
       caption = "Data from WB/IMF, Analysis by Yulin"
       ) +
  # moves the legend from the side (default) to the bottom.
  theme(legend.position = "bottom") 

Chart3: Top10 trde CO2 country in 2018

emissions_dataset %>%
  # filter the year in 2018
  filter(year == 2018) %>%
  # get the top 10 by trade CO2
  slice_max(order_by = trade_co2, n = 10) %>%
  # fct_reorder will sort x by trade co2
  ggplot(aes(x = fct_reorder(.f = country_name,.x = trade_co2,
                             .desc = TRUE), 
              # fill will change the color by whether a country is EM or DM
             y = trade_co2, fill = em_dm)) +
  # just get used to the `stat = identity` thing for bar charts, default is `count`
  geom_bar(stat = "identity") +
  geom_text(aes(label = format(trade_co2, digits = 7, big.mark = ",")), vjust = -0.2) + 
  # expand the y limits so the text doesn't get cut off
  scale_y_continuous(limits = c(0,400), breaks = scales::pretty_breaks(n = 10)) +
  # change the colour
  scale_fill_manual(values = c("Developed Markets" = "002020", "Emerging Markets" = "002000")) +
  # a minimal theme with mostly white space
  theme_minimal() +
  labs(title = "Top 10 Trade CO2 Contry",
      subtitle = "Year 2018",
       x = "Country", 
       y = "Trade CO2",
       fill = "", # this gets rid of the `em_dm` tag on top of the legend,
       caption = "Data from WB/IMF, Analysis by Yulin"
       ) +
  # moves the legend from the side (default) to the bottom.
  theme(legend.position = "bottom")