Introduction

This project investigates the question: How do nations’ climate change impact and economic efficiency compare over time?

The climate crisis is an existential threat that countries around the globe are being forced to confront. In 2018, the U.N. Intergovernmental Panel on Climate Change (IPCC) released a report revealing that the projected temperature increase for 2040, estimated to be 2.7 degrees Fahrenheit, will have detrimental impacts around the globe including submerged coastlines, droughts, and poverty, among many others. Furthermore, the 2021 Global Risks Report by the World Economic Forum reveals that the COVID-19 pandemic will have continuing impacts on the climate as well, including growing inequality and geopolitical instability. The report wrote,

“Climate change—to which no one is immune—continues to be a catastrophic risk. Although lockdowns worldwide caused global emissions to fall in the first half of 2020, evidence from the 2008–2009 financial crisis warns that emissions could bounce back. A shift towards greener economies cannot be delayed until the shocks of the pandemic subside.”

According to the IPCC, avoiding some of the most serious impacts of climate change will require $54 trillion in economic transformation. As such, the future of global climate change as well as individual nations’ climate impact are intrinsically tied to economic efficiency. Studying the state of nations’ contributions to climate change today and over time in relation to their economic well-being sheds key insights on which global players can and should show global leadership in taking substantive and measurable steps to solve the climate crisis.


Data and Methods

The data used in this project is a combination of data sets from various sources. Most of the data came from Gapminder, which is an independent educational nonprofit that has data on a variety of topics including the environment and the economy. For this project, we combined data sets for CO2 emissions per capita, total CO2 emissions, GDP per capita, and population.

The CO2 emissions per capita data, titled “CO2 emissions (tonnes per person),” details CO2 emissions per capita for each country every year from 1800-2018, with consistent data starting in 1960. This project also used data for total CO2 emissions, titled “Total CO2 emissions (1000 tonnes).” This data set details the total amount of CO2 emissions a country emits in a year from 1751-2018, with consistent data starting in 1960. Both these data sets originally come from the Carbon Dioxide Information Analysis Center, which can be found here.

Additionally, this project also used Gapminder data for GDP per capita and population. The GDP per capita, titled “GDP/capita (US$ inflation adjusted)” gives the yearly GDP per capita rates in each country, normalized for US inflation, from 1960-2018. The original source for this data set is the World Bank and can be found here. We also used population data from Gapminder, titled “Population, total.” The data set measures total population for each country from 1800-2100 (projected) for each country. The original source is Gapminder and can be found here.

Finally, used a data frame with countries and their corresponding continents. We got this data from GitHub. As far as we know, the owner of the file created the data frame that we are using and thus it is the original source.

Most of the data used was pre-processed by Gapminder, which is a well-respected organization. As we have used many of their data sets for educational purposes and they are affiliated with R, we believe this data is valid. Additionally, the original sources for the data–The Carbon Dioxide Information Analysis Center and the World Bank–are also well known research and global organizations. The last data set we used to match countries and continents came from an unverified user on GitHub; however, because of the straight-forward nature of the data–simply listing country names with their corresponding continents–we did not find using data from an unverified source problematic.

# Data wrangling
long_co2_emission_per_capita <- co2_emission_per_capita %>%
    pivot_longer(cols = 2:last_col(),
                 names_to = "year", 
                 values_to = "co2_emissions_per_capita")

long_gdp_per_capita <- gdp_per_capita %>%
    pivot_longer(cols = 2:last_col(),
                 names_to = "year", 
                 values_to = "gdp_per_capita")

long_population <- population %>%
    pivot_longer(cols = 2:last_col(),
                 names_to = "year", 
                 values_to = "population")

long_co2_emissions <- co2_emissions %>%
  pivot_longer(cols = 2:last_col(), 
               names_to = "year", 
               values_to = "co2_emissions")

countries_continents <- countries_continents %>%
    clean_names()

data <- long_population %>%
    left_join(long_gdp_per_capita, by = c("country", "year")) %>%
    left_join(long_co2_emission_per_capita, by = c("country", "year")) %>%
    left_join(countries_continents, by = "country") %>%
    left_join(long_co2_emissions, by = c("country", "year")) %>%
    filter(!is.na(gdp_per_capita)) %>%
    filter(!is.na(co2_emissions_per_capita)) %>%
  mutate(year = as.double(year))

data_2018 <- data %>%
  filter(year == 2018)


Findings

China, the United States, India, and Russia are the top four countries contributing to global CO2 emissions today.

CO2 is one of the leading greenhouse gases contributing to climate change today. As such, it is imperative to understand how individual countries contribute to the total amount of CO2 emissions. The following visualization shows the top ten CO2 emitters from 1960-2018, including China, the United States, India, Russia, Japan, Germany, South Korea, Saudi Arabia, Indonesia, and Canada. As you can see, China, the United States, India, and Russia are the top four CO2 emitters, with the United States originally emitting more CO2 than all other countries and China overtaking U.S. levels in the late 2000s. China’s CO2 emissions grew exponentially and far outweighs all other countries.

# Which countries contribute the most to CO2 emissions today?

# Animated line chart
anim_data <- data %>%
  mutate(co2_emissions = (co2_emissions)*10^-6) %>%
  filter(country %in% c("China", "United States", "India", "Russia", "Japan", "Germany", "South Korea", "Saudi Arabia", "Indonesia", "Canada")) 
global_co2_emissions_anim_plot <- anim_data %>%
  ggplot() +
  geom_point(aes(x = year, y = co2_emissions, color = country),
    size = 2) +
  geom_line(aes(x = year, y = co2_emissions, color = country)) +
  geom_text_repel(
    data = anim_data %>%
      filter(country == "China" | country == "United States" | country == "India" | country == "Russia"),
    aes(x = year, y = co2_emissions, color = country, label = country),
                  hjust = 0, nudge_x = 2, direction = "y", 
    segment.color = 'grey', na.rm = TRUE) +
  scale_color_manual(values = c("grey", "red", "grey", "forestgreen", "grey", "grey", "purple", "grey", "grey", "blue")) +
  scale_x_continuous(
    expand = expansion(add = c(0, 15)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018) 
  ) +
  theme_half_open(font_size = 15) +
  labs(
    x = NULL, 
    y = "CO2 emissions (1,000,000 tonnes)",
    title = "Top 10 CO2 Emitting Countries",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, Yearly CO2 Emissions (1000 Tonnes)"
  ) +
  coord_cartesian(clip = 'off') +
  theme(legend.position = 'none')
global_co2_emissions_anim <- global_co2_emissions_anim_plot +
  transition_reveal(year)

animate(global_co2_emissions_anim,
        end_pause = 15,
        duration = 10, 
        width = 1100, height = 650, res = 150,
        renderer = magick_renderer())
anim_save(here::here(
  'figs', 'co2_emissions_line_anim_revised.gif'))


The following visualizations shows the top 25 countries in total CO2 emissions in 2018 in relation to the global mean. As the bar chart shows, CO2 emissions is widely distributed with approximately the top 20 countries surpassing the global mean and all other countries either meeting the global mean or being below it. This reveals that the amount of CO2 nations emit is greatly unequal, with top contributors like China, the United States, India, and Russia making substantial contributions that are impacting the rest of the globe. In attempting to address climate change, this suggests that some countries have more responsibility than others in making substantive policies to decrease their greenhouse gas emissions.

# Bar chart

# mean(data_2018$co2_emissions)

data_2018 %>%
  mutate(
    co2_emissions = (co2_emissions)*(10^-6),
    is_top4 = if_else(
      country %in% c("China", "United States", "India", "Russia"), TRUE, FALSE),
    ) %>%
  arrange(desc(co2_emissions)) %>%
  slice(1:25) %>%
  ggplot() +
  geom_col(aes(
    x = co2_emissions, 
    y = reorder(country, co2_emissions),
    fill = is_top4),
    width = 0.7, alpha = 0.8
  ) +
  geom_vline(
    xintercept = .1864441,
    color = 'darkred', linetype = 'dashed'
    ) +
  scale_x_continuous(expand = expand_scale(mult = c(0, 0.05))) +
  labs(
    x = "CO2 emissions (1,000,000 tonnes)",
    y = NULL, 
    title = "Top 25 countries in total CO2 emissions in 2018",
    caption = "Source: Gapminder, Yearly CO2 Emissions (1000 Tonnes)"
  ) +
  scale_fill_manual(values = c('grey', 'steelblue')) +
  annotate(
    'text', x = .5, y = 'Malaysia',
    color = 'darkred', hjust = 0, 
    label = 'Mean global CO2 \nemissions'
  ) +
  theme_minimal_vgrid() +
  theme(legend.position = 'none')

As nations become more economically efficient, their CO2 emissions rise.

One of the key factors in addressing the climate crisis is nations’ abilities to decouple their relationship between economic growth and fossil fuel emissions. Ideally, well-off nations with high economic efficiency would put increased effort into decreasing their fossil fuel emissions contrary to what capitalist systems demand. However, when plotting GDP per capita and CO2 emissions per capita against each other, nations show a relatively clear positive relationship between the two variables. The United States, which is one of the countries with a consistently high GDP per capita, continues to also rise in CO2 emissions per capita. All countries show a general upward trend with CO2 emissions per capita rising as GDP per capita rises from 1960 to 2018.

# What is the relationship between CO2 emissions per capita and GDP per capita? 

# Animated scatterplot
co2_gdp_plot <- plot_ly(
  data = data,
  type = 'scatter',
  x = ~gdp_per_capita,
  y = ~co2_emissions_per_capita,
  size = ~population,
  color = ~continent,
  text = ~country,
  frame = ~year, 
  mode = "markers",
  sizes = c(10, 2000),
  marker = list(opacity = 0.5),
  hoverinfo = "text"
  ) %>%
  layout(xaxis = list(type = "log"))

x <- list(
  title = "GDP per capita"
)

y <- list(
  title = "CO2 emissions per capita (tonnes)"
)

co2_gdp_plot <- co2_gdp_plot %>%
  layout(
    xaxis = x, 
    yaxis = y)
  
co2_gdp_plot <- co2_gdp_plot %>%
  layout(
    title = 'As GDP per capita increases, CO2 emissions per capita also increase',
    yaxis = list(range = c(0, 40)))

co2_gdp_plot

Source: Gapminder, CO2 Emissions (Tonnes) Per Person, GDP Per Capita (US Inflation Adjusted)

htmlwidgets::saveWidget(
  ggplotly(co2_gdp_plot),
  file = here::here('figs', 'co2_gdp_plot.html')
)
gdp_1960 <- data %>%
  filter(year == "1960") %>%
  select(country, gdp_1960 = gdp_per_capita)

co2_1960 <- data %>%
  filter(year == "1960") %>%
  select(country, co2_1960 = co2_emissions_per_capita)

data <- data %>%
  left_join(gdp_1960, by = "country") %>%
  left_join(co2_1960, by = "country") %>%
  group_by(country) %>%
  mutate(
    gdp_percent_growth = (gdp_per_capita - gdp_1960)/gdp_1960,
    co2_percent_growth = (co2_emissions_per_capita - co2_1960)/co2_1960
  ) 

data_percent_growth <- data %>%
  pivot_longer(cols = gdp_percent_growth:co2_percent_growth,
               names_to = "growth_area",
               values_to = "percent_growth") %>%
  mutate(
    growth_area = fct_recode(growth_area,
          'GDP per capita' = 'gdp_percent_growth',
          'CO2 emissions per capita' = 'co2_percent_growth')
  )

China’s economic growth has risen exponentially since 1960, but their CO2 emissions per capita growth only started to slightly plateau in 2010.

As one of the leading contributors to CO2 emissions globally, China’s climate policies are extremely important. China’s total CO2 emissions in 2018 alone almost doubled the United States’ total emissions and far outweighs all other countries. Plotting China’s GDP per capita growth and CO2 emissions per capita growth from 1960 levels, we can see that their GDP per capita has increased exponentially, reaching an almost 4000% increase. Their CO2 emissions per capita growth has also increased substantially, reaching an almost 500% increase from 1960. Though this is much lower than their GDP per capita growth rate, it is important to note the sheer amount of CO2 emissions that entails, particularly considering the population of China.

However, as the visualizations reveals, China’s CO2 emissions per capita growth rate started plateauing starting around 2010. Though we can still see a slight increase in the CO2 emissions per capita growth rate, the ratio of GDP per capita growth to CO2 emissions per capita growth is quite large and somewhat represents the relationship needed between economic growth and fossil fuel emissions in order to address climate change. Going forward, much more drastic measures must be put into place so we can achieve a negative relationship between the two variables, with the growth rate for GDP per capita increasing but CO2 emissions per capita decreasing.

data_percent_growth %>%
  filter(country == "China") %>%
  ggplot() +
  geom_line(aes(x = year, y = percent_growth, color = growth_area)) +
  geom_text_repel(
    data = data_percent_growth %>%
      filter(country == "China" & year == "2018"),
    aes(x = year, y = percent_growth, label = growth_area, color = growth_area),
    hjust = 0, nudge_x = 1, direction = "y",
    size = 4, segment.color = NA
  ) +
  scale_x_continuous(
    expand = expansion(add = c(0, 25)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018)
  ) +
  scale_color_manual(values = c("red", "blue")) +
  labs(
    x = NULL, 
    y = "Percent growth from 1960",
    title = "Percent growth of CO2 per capita and GDP per capita from \n1960 in China",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, CO2 Emissions (Tonnes) Per Person, \nGDP Per Capita (US Inflation Adjusted)"
  ) +
  theme_minimal_hgrid() +
  theme(legend.position = 'none')


The United States’ economic growth has increased at a steady rate since 1960 and CO2 emissions per capita growth has decreased at an unsteady rate.

The United States is also a key nation contributing to climate change. As the second largest contributor for CO2 emissions in 2018 and one of the countries with a consistently high GDP per capita since 1960, it is imperative that the United States lead the way in making economic transformations to address the climate crisis. As the visualization reveals, the U.S.’s GDP per capita growth rate has increased steadily from 1960 to 2018. Though CO2 emissions per capita growth rate spiked from 1960 to the mid-1970s, it shows a general decreasing trend ever since.

Though this shows an encouraging relationship between economic growth and fossil fuel emissions, it is important to note that, like China, the sheer amount of CO2 emissions the United States is producing is still enormous. Though the CO2 emissions per capita growth rate only shows a 7% increase in 2018 from 1960, that still amounts to 5,000,000 tonnes of total CO2 emissions in 2018, which far outweighs other countries excluding China. While the relationship shown illustrates an encouraging picture, the United States still must take drastic measures to continue decreasing their CO2 emissions growth rate.

data_percent_growth %>%
  filter(country == "United States") %>%
  ggplot() +
  geom_line(aes(x = year, y = percent_growth, color = growth_area)) +
  geom_text_repel(
    data = data_percent_growth %>%
      filter(country == "United States" & year == "2018"),
    aes(x = year, y = percent_growth, label = growth_area, color = growth_area),
    hjust = 0, nudge_x = 1, direction = "y",
    size = 4, segment.color = NA
  ) +
  scale_x_continuous(
    expand = expansion(add = c(0, 25)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018)
  ) +
  scale_color_manual(values = c("red", "blue")) +
  labs(
    x = NULL, 
    y = "Percent growth from 1960",
    title = "Percent growth of CO2 per capita and GDP per capita from \n1960 in United States",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, CO2 Emissions (Tonnes) Per Person, \nGDP Per Capita (US Inflation Adjusted)"
  ) +
  theme_minimal_hgrid() +
  theme(legend.position = 'none')


Burundi’s economic growth has stayed on par with 1960 levels but their CO2 emissions per capita growth has spiked to 260% of 1960 levels.

When investigating the relationship between GDP per capita and CO2 emissions per capita, Burundi was one of the countries with the lowest GDP per capita and CO2 emissions per capita. In exploring the growth rate of CO2 emissions per capita and GDP per capita from 1960 to 2018, we can see that their GDP per capita is in a similar state as in 1960, suggesting they have had limited economic growth; however, despite this, their CO2 emissions per capita has increased unevenly, now resting at approximately 260% of their 1960 levels. Despite having low economic efficiency, they are still emitting CO2 at increased levels. This is indicative of countries emitting more CO2 regardless of economic efficiency and CO2 emission patterns for countries with low GDP per capita.

data_percent_growth %>%
  filter(country == "Burundi") %>%
  ggplot() +
  geom_line(aes(x = year, y = percent_growth, color = growth_area)) +
  geom_text_repel(
    data = data_percent_growth %>%
      filter(country == "Burundi" & year == "2018"),
    aes(x = year, y = percent_growth, label = growth_area, color = growth_area),
    hjust = 0, nudge_x = 1, direction = "y",
    size = 4, segment.color = NA
  ) +
  scale_x_continuous(
    expand = expansion(add = c(0, 25)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018)
  ) +
  scale_color_manual(values = c("red", "blue")) +
  labs(
    x = NULL, 
    y = "Percent growth from 1960",
    title = "Percent growth of CO2 per capita and GDP per capita from \n1960 in Burundi",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, CO2 Emissions (Tonnes) Per Person, \nGDP Per Capita (US Inflation Adjusted)"
  ) +
  theme_minimal_hgrid() +
  theme(legend.position = 'none')


Conclusion

This project sought to understand the current state of countries’ contributions to CO2 emissions and the relationship between countries’ climate impact and economic efficiency. From our analysis, we found that the leading five countries in CO2 emissions in 2018 are China, the United States, India, Russia, and Japan. We also found that globally, as nations’ GDP per capita increases, their CO2 emissions also increase. As nations continue to address the climate crisis, it is imperative that they decouple this positive relationship between economic growth and fossil fuel emissions, and work towards decreasing their fossil fuel emissions as economic growth increases. By computing the growth rate for GDP per capita and CO2 emissions since 1960, we found that though both China and the United States display favorable relationships between GDP per capita growth rate and CO2 emissions per capita growth rate–with GDP per capita growing and CO2 emissions per capita plateauing–the sheer amount of CO2 emissions they are both emitting still severely outweighs all other countries. In investigating the relationship between economic growth and fossil fuel emission growth in Burundi, we found that countries with lower GDP per capita growth rate are still emitting CO2 at a much higher rate despite being one of the lowest CO2 emitters globally. This is indicative of the performance of less economically well-off countries.

This project was limited in its scope of research. It primarily investigated CO2 emissions and GDP per capita as markers of nations’ climate impact and economic growth. Future research should be done to investigate other markers, such as other greenhouse gas emissions. Furthermore, this project only investigates the state of nations’ climate impact today and does not explore any policy implications in further detail in order to achieve the desired relationships discussed here. As we continue to face the effects of climate change, more research must be done to find the best way nations can respond in a global effort.


Appendix A: Data Dictionary

kable(data_dictionary_revised)
Variable Type Description Source
country Character Country name Gapminder, CO2 Emissions (Tonnes) Per Person
year Double Year by increments of one Gapminder, CO2 Emissions (Tonnes) Per Person
population Double Population of the given country Gapminder, Population total
gdp_per_capita Double GDP per capita, adjusted for US inflation Gapminder, GDP Per Capita (US Inflation Adjusted)
co2_emissions_per_capita Double CO2 emissions per capita, metric tonnes Gapminder, CO2 Emissions (Tonnes) Per Person
continent Character Country continent GitHub, dbouquin, Countries-Continents.csv
co2_emissions_per_capita Double Total CO2 emissions from the given country, per 1000 tonnes Yearly CO2 Emissions (1000 Tonnes)
gdp_1960 Double GDP per capita in 1960 Computed; Gapminder, GDP Per Capita (US Inflation Adjusted)
co2_1960 Double CO2 emissions per capita in 1960 Computed; Gapminder, CO2 Emissions (Tonnes) Per Person
growth_area Factor CO2 emissions per capita or GDP per capita Computed
percent_growth Double Percent growth in GDP per capita and CO2 emissions per capita from 1960 levels Computed

Appendix B:Code

# Load libraries and settings 
# install.packages("gganimate")
# install.packages("magick")
library(tidyverse)
library(here)
library(janitor)
library(plotly)
library(readxl)
library(maps)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(cowplot)
library(leaflet)
library(viridis)
library(ggrepel)
library(knitr)
# library(gganimate)
# library(magick)


knitr::opts_chunk$set(
    warning = FALSE,
    message = FALSE,
    comment = "#>",
    fig.path = "figs/", # Folder where rendered plots are saved
    fig.width = 7.252, # Default plot width
    fig.height = 4, # Default plot height
    fig.retina = 3 # For better plot resolution
)

# Load data 
gdp_per_capita <- read_csv(here::here('data', 'gdppercapita_us_inflation_adjusted.csv'))
population <- read_csv(here::here('data', 'population_total.csv'))
co2_emission_per_capita <- read_csv(here::here('data', 'co2_emissions_tonnes_per_person.csv'))
countries_continents <- read_csv(here::here('data', 'Countries-Continents.csv'))
co2_emissions <- read_csv(here::here('data', 'yearly_co2_emissions_1000_tonnes.csv'))

data_dictionary_path <- here('data','data_dictionary_final.xlsx')
data_dictionary_co2 <- read_excel(data_dictionary_path, sheet = 'co2_emissions')
data_dictionary_gdp <- read_excel(data_dictionary_path, sheet = 'gdp')
data_dictionary_countries <- read_excel(data_dictionary_path, sheet = 'countries_continents')
data_dictionary_population <- read_excel(data_dictionary_path, sheet = 'population_dictionary')

data_dictionary_revised <- read_excel(here::here('data','data_dictionary_revised.xlsx'))

# Put any other "global" settings here, e.g. a ggplot theme:
# theme_set(theme_bw(base_size = 20))
# Data wrangling
long_co2_emission_per_capita <- co2_emission_per_capita %>%
    pivot_longer(cols = 2:last_col(),
                 names_to = "year", 
                 values_to = "co2_emissions_per_capita")

long_gdp_per_capita <- gdp_per_capita %>%
    pivot_longer(cols = 2:last_col(),
                 names_to = "year", 
                 values_to = "gdp_per_capita")

long_population <- population %>%
    pivot_longer(cols = 2:last_col(),
                 names_to = "year", 
                 values_to = "population")

long_co2_emissions <- co2_emissions %>%
  pivot_longer(cols = 2:last_col(), 
               names_to = "year", 
               values_to = "co2_emissions")

countries_continents <- countries_continents %>%
    clean_names()

data <- long_population %>%
    left_join(long_gdp_per_capita, by = c("country", "year")) %>%
    left_join(long_co2_emission_per_capita, by = c("country", "year")) %>%
    left_join(countries_continents, by = "country") %>%
    left_join(long_co2_emissions, by = c("country", "year")) %>%
    filter(!is.na(gdp_per_capita)) %>%
    filter(!is.na(co2_emissions_per_capita)) %>%
  mutate(year = as.double(year))

data_2018 <- data %>%
  filter(year == 2018)

# Which countries contribute the most to CO2 emissions today?

# Animated line chart
anim_data <- data %>%
  mutate(co2_emissions = (co2_emissions)*10^-6) %>%
  filter(country %in% c("China", "United States", "India", "Russia", "Japan", "Germany", "South Korea", "Saudi Arabia", "Indonesia", "Canada")) 
global_co2_emissions_anim_plot <- anim_data %>%
  ggplot() +
  geom_point(aes(x = year, y = co2_emissions, color = country),
    size = 2) +
  geom_line(aes(x = year, y = co2_emissions, color = country)) +
  geom_text_repel(
    data = anim_data %>%
      filter(country == "China" | country == "United States" | country == "India" | country == "Russia"),
    aes(x = year, y = co2_emissions, color = country, label = country),
                  hjust = 0, nudge_x = 2, direction = "y", 
    segment.color = 'grey', na.rm = TRUE) +
  scale_color_manual(values = c("grey", "red", "grey", "forestgreen", "grey", "grey", "purple", "grey", "grey", "blue")) +
  scale_x_continuous(
    expand = expansion(add = c(0, 15)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018) 
  ) +
  theme_half_open(font_size = 15) +
  labs(
    x = NULL, 
    y = "CO2 emissions (1,000,000 tonnes)",
    title = "Top 10 CO2 Emitting Countries",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, Yearly CO2 Emissions (1000 Tonnes)"
  ) +
  coord_cartesian(clip = 'off') +
  theme(legend.position = 'none')

global_co2_emissions_anim <- global_co2_emissions_anim_plot +
  transition_reveal(year)

animate(global_co2_emissions_anim,
        end_pause = 15,
        duration = 10, 
        width = 1100, height = 650, res = 150,
        renderer = magick_renderer())
anim_save(here::here(
  'figs', 'co2_emissions_line_anim_revised.gif'))
# Bar chart

# mean(data_2018$co2_emissions)

data_2018 %>%
  mutate(
    co2_emissions = (co2_emissions)*(10^-6),
    is_top4 = if_else(
      country %in% c("China", "United States", "India", "Russia"), TRUE, FALSE),
    ) %>%
  arrange(desc(co2_emissions)) %>%
  slice(1:25) %>%
  ggplot() +
  geom_col(aes(
    x = co2_emissions, 
    y = reorder(country, co2_emissions),
    fill = is_top4),
    width = 0.7, alpha = 0.8
  ) +
  geom_vline(
    xintercept = .1864441,
    color = 'darkred', linetype = 'dashed'
    ) +
  scale_x_continuous(expand = expand_scale(mult = c(0, 0.05))) +
  labs(
    x = "CO2 emissions (1,000,000 tonnes)",
    y = NULL, 
    title = "Top 25 countries in total CO2 emissions in 2018",
    caption = "Source: Gapminder, Yearly CO2 Emissions (1000 Tonnes)"
  ) +
  scale_fill_manual(values = c('grey', 'steelblue')) +
  annotate(
    'text', x = .5, y = 'Malaysia',
    color = 'darkred', hjust = 0, 
    label = 'Mean global CO2 \nemissions'
  ) +
  theme_minimal_vgrid() +
  theme(legend.position = 'none')
# What is the relationship between CO2 emissions per capita and GDP per capita? 

# Animated scatterplot
co2_gdp_plot <- plot_ly(
  data = data,
  type = 'scatter',
  x = ~gdp_per_capita,
  y = ~co2_emissions_per_capita,
  size = ~population,
  color = ~continent,
  text = ~country,
  frame = ~year, 
  mode = "markers",
  sizes = c(10, 2000),
  marker = list(opacity = 0.5),
  hoverinfo = "text"
  ) %>%
  layout(xaxis = list(type = "log"))

x <- list(
  title = "GDP per capita"
)

y <- list(
  title = "CO2 emissions per capita (tonnes)"
)

co2_gdp_plot <- co2_gdp_plot %>%
  layout(
    xaxis = x, 
    yaxis = y)
  
co2_gdp_plot <- co2_gdp_plot %>%
  layout(
    title = 'As GDP per capita increases, CO2 emissions per capita also increase',
    yaxis = list(range = c(0, 40)))

co2_gdp_plot
htmlwidgets::saveWidget(
  ggplotly(co2_gdp_plot),
  file = here::here('figs', 'co2_gdp_plot.html')
)
gdp_1960 <- data %>%
  filter(year == "1960") %>%
  select(country, gdp_1960 = gdp_per_capita)

co2_1960 <- data %>%
  filter(year == "1960") %>%
  select(country, co2_1960 = co2_emissions_per_capita)

data <- data %>%
  left_join(gdp_1960, by = "country") %>%
  left_join(co2_1960, by = "country") %>%
  group_by(country) %>%
  mutate(
    gdp_percent_growth = (gdp_per_capita - gdp_1960)/gdp_1960,
    co2_percent_growth = (co2_emissions_per_capita - co2_1960)/co2_1960
  ) 

data_percent_growth <- data %>%
  pivot_longer(cols = gdp_percent_growth:co2_percent_growth,
               names_to = "growth_area",
               values_to = "percent_growth") %>%
  mutate(
    growth_area = fct_recode(growth_area,
          'GDP per capita' = 'gdp_percent_growth',
          'CO2 emissions per capita' = 'co2_percent_growth')
  )

data_percent_growth %>%
  filter(country == "China") %>%
  ggplot() +
  geom_line(aes(x = year, y = percent_growth, color = growth_area)) +
  geom_text_repel(
    data = data_percent_growth %>%
      filter(country == "China" & year == "2018"),
    aes(x = year, y = percent_growth, label = growth_area, color = growth_area),
    hjust = 0, nudge_x = 1, direction = "y",
    size = 4, segment.color = NA
  ) +
  scale_x_continuous(
    expand = expansion(add = c(0, 25)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018)
  ) +
  scale_color_manual(values = c("red", "blue")) +
  labs(
    x = NULL, 
    y = "Percent growth from 1960",
    title = "Percent growth of CO2 per capita and GDP per capita from \n1960 in China",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, CO2 Emissions (Tonnes) Per Person, \nGDP Per Capita (US Inflation Adjusted)"
  ) +
  theme_minimal_hgrid() +
  theme(legend.position = 'none')
data_percent_growth %>%
  filter(country == "United States") %>%
  ggplot() +
  geom_line(aes(x = year, y = percent_growth, color = growth_area)) +
  geom_text_repel(
    data = data_percent_growth %>%
      filter(country == "United States" & year == "2018"),
    aes(x = year, y = percent_growth, label = growth_area, color = growth_area),
    hjust = 0, nudge_x = 1, direction = "y",
    size = 4, segment.color = NA
  ) +
  scale_x_continuous(
    expand = expansion(add = c(0, 25)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018)
  ) +
  scale_color_manual(values = c("red", "blue")) +
  labs(
    x = NULL, 
    y = "Percent growth from 1960",
    title = "Percent growth of CO2 per capita and GDP per capita from \n1960 in United States",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, CO2 Emissions (Tonnes) Per Person, \nGDP Per Capita (US Inflation Adjusted)"
  ) +
  theme_minimal_hgrid() +
  theme(legend.position = 'none')

data_percent_growth %>%
  filter(country == "Burundi") %>%
  ggplot() +
  geom_line(aes(x = year, y = percent_growth, color = growth_area)) +
  geom_text_repel(
    data = data_percent_growth %>%
      filter(country == "Burundi" & year == "2018"),
    aes(x = year, y = percent_growth, label = growth_area, color = growth_area),
    hjust = 0, nudge_x = 1, direction = "y",
    size = 4, segment.color = NA
  ) +
  scale_x_continuous(
    expand = expansion(add = c(0, 25)),
    limits = c(1960, 2018),
    breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2018)
  ) +
  scale_color_manual(values = c("red", "blue")) +
  labs(
    x = NULL, 
    y = "Percent growth from 1960",
    title = "Percent growth of CO2 per capita and GDP per capita from \n1960 in Burundi",
    subtitle = "1960-2018",
    caption = "Source: Gapminder, CO2 Emissions (Tonnes) Per Person, \nGDP Per Capita (US Inflation Adjusted)"
  ) +
  theme_minimal_hgrid() +
  theme(legend.position = 'none')
kable(data_dictionary_revised)