packages <- c("tidyverse", "srvyr", "broom","gt", "modelsummary", 
"gapminder", "fst", "ggridges", "readxl", "readr")  
 
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]  
if(length(new_packages)) install.packages(new_packages)  
lapply(packages, library, character.only = TRUE)  
## ── 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.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ 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
## 
## Attaching package: 'srvyr'
## 
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## 
## `modelsummary` 2.0.0 now uses `tinytable` as its default table-drawing
##   backend. Learn more at: https://vincentarelbundock.github.io/tinytable/
## 
## Revert to `kableExtra` for one session:
## 
##   options(modelsummary_factory_default = 'kableExtra')
##   options(modelsummary_factory_latex = 'kableExtra')
##   options(modelsummary_factory_html = 'kableExtra')
## 
## Silence this message forever:
## 
##   config_modelsummary(startup_message = FALSE)
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "srvyr"     "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "broom"     "srvyr"     "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[4]]
##  [1] "gt"        "broom"     "srvyr"     "lubridate" "forcats"   "stringr"  
##  [7] "dplyr"     "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"  
## [13] "tidyverse" "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [19] "methods"   "base"     
## 
## [[5]]
##  [1] "modelsummary" "gt"           "broom"        "srvyr"        "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[6]]
##  [1] "gapminder"    "modelsummary" "gt"           "broom"        "srvyr"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "fst"          "gapminder"    "modelsummary" "gt"           "broom"       
##  [6] "srvyr"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "ggridges"     "fst"          "gapminder"    "modelsummary" "gt"          
##  [6] "broom"        "srvyr"        "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "readxl"       "ggridges"     "fst"          "gapminder"    "modelsummary"
##  [6] "gt"           "broom"        "srvyr"        "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "readxl"       "ggridges"     "fst"          "gapminder"    "modelsummary"
##  [6] "gt"           "broom"        "srvyr"        "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"
GHG_emissions <- read_excel("co2_data.xlsx")

glimpse(GHG_emissions)
## Rows: 50,191
## Columns: 79
## $ country                                   <chr> "Afghanistan", "Afghanistan"…
## $ year                                      <dbl> 1750, 1751, 1752, 1753, 1754…
## $ iso_code                                  <chr> "AFG", "AFG", "AFG", "AFG", …
## $ population                                <dbl> 2802560, NA, NA, NA, NA, NA,…
## $ gdp                                       <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cement_co2                                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ cement_co2_per_capita                     <dbl> 0, NA, NA, NA, NA, NA, NA, N…
## $ co2                                       <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_growth_abs                            <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_growth_prct                           <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_including_luc                         <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_including_luc_growth_abs              <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_including_luc_growth_prct             <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_including_luc_per_capita              <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_including_luc_per_gdp                 <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_including_luc_per_unit_energy         <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_per_capita                            <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_per_gdp                               <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ co2_per_unit_energy                       <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ coal_co2                                  <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ coal_co2_per_capita                       <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ consumption_co2                           <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ consumption_co2_per_capita                <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ consumption_co2_per_gdp                   <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_cement_co2                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ cumulative_co2                            <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_co2_including_luc              <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_coal_co2                       <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_flaring_co2                    <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_gas_co2                        <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_luc_co2                        <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_oil_co2                        <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ cumulative_other_co2                      <lgl> NA, NA, NA, NA, NA, NA, NA, …
## $ energy_per_capita                         <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ energy_per_gdp                            <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ flaring_co2                               <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ flaring_co2_per_capita                    <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ gas_co2                                   <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ gas_co2_per_capita                        <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ ghg_excluding_lucf_per_capita             <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ ghg_per_capita                            <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ land_use_change_co2                       <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ land_use_change_co2_per_capita            <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ methane                                   <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ methane_per_capita                        <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ nitrous_oxide                             <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ nitrous_oxide_per_capita                  <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ oil_co2                                   <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ oil_co2_per_capita                        <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ other_co2_per_capita                      <lgl> NA, NA, NA, NA, NA, NA, NA, …
## $ other_industry_co2                        <lgl> NA, NA, NA, NA, NA, NA, NA, …
## $ primary_energy_consumption                <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cement_co2                   <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_co2                          <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_co2_including_luc            <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_coal_co2                     <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_cement_co2        <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_co2               <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_co2_including_luc <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_coal_co2          <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_flaring_co2       <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_gas_co2           <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_luc_co2           <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_oil_co2           <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_cumulative_other_co2         <lgl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_flaring_co2                  <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_gas_co2                      <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_luc_co2                      <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_oil_co2                      <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_global_other_co2                    <lgl> NA, NA, NA, NA, NA, NA, NA, …
## $ share_of_temperature_change_from_ghg      <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ temperature_change_from_ch4               <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ temperature_change_from_co2               <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ temperature_change_from_ghg               <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ temperature_change_from_n2o               <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ total_ghg                                 <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ total_ghg_excluding_lucf                  <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ trade_co2                                 <dbl> NA, NA, NA, NA, NA, NA, NA, …
## $ trade_co2_share                           <dbl> NA, NA, NA, NA, NA, NA, NA, …
year_range <- range(GHG_emissions$year)

print(year_range)
## [1] 1750 2023
co2consumption_by_countries <- GHG_emissions %>%
  filter(year >= 1980 & year <= 2023) %>%
  filter(!is.na(consumption_co2))%>%
  filter(country %in% c("Canada", "Brazil", "Germany")) %>%
  select(year, country, consumption_co2) %>%
  group_by(country) %>%
  arrange(desc(consumption_co2))

print(co2consumption_by_countries)
## # A tibble: 99 × 3
## # Groups:   country [3]
##     year country consumption_co2
##    <dbl> <chr>             <dbl>
##  1  1992 Germany           1207.
##  2  1990 Germany           1195.
##  3  1993 Germany           1160.
##  4  1991 Germany           1144.
##  5  1995 Germany           1140.
##  6  1994 Germany           1135.
##  7  1996 Germany           1124.
##  8  1999 Germany           1090.
##  9  2000 Germany           1084.
## 10  2001 Germany           1081.
## # ℹ 89 more rows
ggplot( 
  co2consumption_by_countries, 
aes(x = year, y = consumption_co2, 
color = country, fill = country) 
) +  
geom_line(linewidth = 1.5) + 
scale_color_brewer(palette = "Set1") + 
theme_minimal() + 
theme( 
panel.grid = element_blank(), 
plot.title = element_text(face = "bold", size = 14), 
plot.subtitle = element_text(size = 12), 
legend.position = "bottom",
legend.title = element_text(face = "bold", size = 12)
) + 
labs( 
title = "Consumption-based CO2 emissions", 
subtitle = "in Selected Countries", 
x = "Year", 
y = "CO2 consumption (measured in million tonnes)", 
color = "Country",
fill = "Country"
) 

co2consumption_by_countries_pc <- GHG_emissions %>%
  filter(year >= 1980 & year <= 2023) %>%
  filter(!is.na(consumption_co2_per_capita))%>%
  filter(country %in% c("Canada", "Brazil", "Germany")) %>%
  select(year, country, consumption_co2_per_capita) %>%
  group_by(country) %>%
  arrange(desc(consumption_co2_per_capita))

print(co2consumption_by_countries_pc)
## # A tibble: 99 × 3
## # Groups:   country [3]
##     year country consumption_co2_per_capita
##    <dbl> <chr>                        <dbl>
##  1  2000 Canada                        18.8
##  2  2005 Canada                        18.7
##  3  2002 Canada                        18.5
##  4  2004 Canada                        18.2
##  5  2003 Canada                        18.1
##  6  2007 Canada                        18.1
##  7  2001 Canada                        17.9
##  8  1997 Canada                        17.9
##  9  1998 Canada                        17.8
## 10  2010 Canada                        17.7
## # ℹ 89 more rows
ggplot( 
  co2consumption_by_countries_pc, 
aes(x = year, y = consumption_co2_per_capita, 
color = country, fill = country) 
) +  
geom_line(linewidth = 1.5) + 
scale_color_brewer(palette = "Set1") + 
theme_minimal() + 
theme( 
panel.grid = element_blank(), 
plot.title = element_text(face = "bold", size = 14), 
plot.subtitle = element_text(size = 12), 
legend.position = "bottom",
legend.title = element_text(face = "bold", size = 12)
) + 
labs( 
title = "Consumption-based CO2 emissions Per Capita", 
subtitle = "in Selected Countries", 
x = "Year", 
y = "CO2 consumption (measured in million tonnes)", 
color = "Country",
fill = "Country"
)