project 1 110 sum

Author

Thyda Khan

Introduction

CO₂ emissions measure how much carbon dioxide a country emits annually from fossil fuels, industrial industries, and energy use, not just from the airline industry

My project examines carbon dioxide (CO₂) emissions across countries from 200-2014. The dataset contains information about individual participating countries, years, and CO₂ emission level. The Country Name variable identifies the country and is categorical, while year and co2 are quantitative variables. This project explores differences in total emissions between countries as well as changes in emissions over time. The original source of the data is: World Bank Open Data Unit: Million metric tons of CO₂ (MtCO₂) Each row represents one country’s total CO₂ emissions for a given year, Vairables: Country Name, Country Code, Year, CO₂

*My focus is from 2000-2014

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.5.2
✔ ggplot2   4.0.3     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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
# Import the CO2 emissions dataset
co2 <- readr::read_csv("co2.csv")
Rows: 3732 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Country Name, Country Code
dbl (2): year, co2

ℹ 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.
my_data <- read_csv("co2.csv")
Rows: 3732 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Country Name, Country Code
dbl (2): year, co2

ℹ 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.
#Explore the set
str(df)
function (x, df1, df2, ncp, log = FALSE)  
head(df)
                                              
1 function (x, df1, df2, ncp, log = FALSE)    
2 {                                           
3     if (missing(ncp))                       
4         .Call(C_df, x, df1, df2, log)       
5     else .Call(C_dnf, x, df1, df2, ncp, log)
6 }                                           
# Clean data
library(tidyverse)

#  Inspect the loaded dataset columns
glimpse(df)
function (x, df1, df2, ncp, log = FALSE)  
# Remove rows with missing years

df_clean <- co2 %>%  mutate(co2 = replace_na(co2, median(co2, na.rm = TRUE))) %>% 
drop_na(year)

#Verify total remaining NA equals 0
sum(is.na(df_clean))
[1] 0

#Exploratory graphs

#Calculate global average CO2 per year and plot it

df_clean %>%
  group_by(year) %>%
  summarise(mean_co2 = mean(co2, na.rm = TRUE)) %>%
  ggplot(aes(x = year, y = mean_co2)) +
  geom_line(color = "pink", size = 3) +
  geom_point(color = "orange") +
  labs(title = "Global Average Co2 Emissions Over Time", x = "Year", y = "Mean Co2") +
  theme_dark()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

# Find the top 10 highest emitting countries on average
df_clean %>%
  group_by(`Country Name`) %>%
  summarise(
    avg_co2 = mean(co2),
    median_co2 = median(co2),
    max_co2 = max(co2)
  ) %>%
  arrange(desc(avg_co2)) %>%
  head(10)
# A tibble: 10 × 4
   `Country Name`            avg_co2 median_co2 max_co2
   <chr>                       <dbl>      <dbl>   <dbl>
 1 Qatar                        52.1       53.2    67.1
 2 Curacao                      37.0       37.7    39.4
 3 Trinidad and Tobago          30.1       33.7    36.1
 4 Kuwait                       28.9       28.8    31.4
 5 United Arab Emirates         24.2       23.3    35.7
 6 Aruba                        22.9       25.9    27.9
 7 Bahrain                      22.9       22.4    28.1
 8 Luxembourg                   21.3       21.1    24.8
 9 Sint Maarten (Dutch part)    19.7       19.5    20.1
10 United States                18.4       19.1    20.2
# Detailed visualization to show the average global Co2 by year
global_trend <- df_clean %>% group_by(year) %>%
  summarise(co2 = mean(co2, na.rm = TRUE))

ggplot(global_trend, aes(x = year, y = co2)) +
  geom_line(linewidth = 2.1) +
  geom_point(size = 3) +
  labs( title = "Global Average Co2 Emissions Over Time", x = "Year",
    y = "Average Co2 Emissions"
  
    ) + 
  theme_dark() +
  theme(
    panel.grid.major = element_line(color = "gray80"),
    panel.grid.minor = element_blank()
  )

library(ggplot2)
library(dplyr)


countries <- c( "United States",
  "Qatar",
  "Trinidad and Tobago",
  "Kuwait",
  "United Arab Emirates")

subset <- df_clean %>%
  filter(`Country Name` %in% countries)

ggplot( subset,
  aes(
    x = year,
    y = co2,
    color = `Country Name`
  )) +
  geom_line(linewidth = .9) +
  geom_point(size = 2.0) +

  scale_color_manual(
    values = c( "United States" = "#1E90FF" ,
                "Qatar" = "#CDC673",
                "Trinidad and Tobago" = "#FFF0F7",  "Kuwait" = "#E9C46A",
     "United Arab Emirates" = "#8E44AD"
    )) +

  labs( title = "Co2 Emission Trends by Country",
    subtitle = "Annual emissions across five selected countries",
  x = "Year", y = "Co2 Emissions",color = "Country",
 caption = "Source: Our World in Data" ) +
  theme_light() 

#Reflection

Limitations-

Data cleaning- Before analyzing my data I inspected the data set to better introduce myself to the variables, noticeable patterns, and missing values that may skew data. I then employed R to Remove rows with missing years and verify that the total remaining NA values equal 0. I as well translated the stored format of “Country” from quantitative to a caterogical variable.In doing these cruicial steps I am getting aqquainted to the data and then applying the necessary cleaning to mitigate room for error.

Findings:

The final visualization represents annual CO₂ emissions for five selected countries over time. Each country is represented by a different color to compare their emission trends.Trinidad emissions remained relatively stable, with some fluctuations without a major upward or downward trend.Kuwait sees a decrease from 2012-2014. The UAE started with high emissions in 2000, but there was a steady decline over time, reaching its lowest around 2012–2013. Qatar emissions peaked around 2001 and again in 2006, followed by a noticeable decline after 2007, then some fluctuation toward the end. The most noticeable patterns is seen in Qatar having the highest Co2 emissions among the five countries for most of the years shown.

Limitations and Further Analysis-

I wanted to compare the CO₂ emission trends of all countries in the dataset, but including every country in one line graph would have made the visualization difficult to visually consume. There would have been too many lines and categories in the legend, making it difficult to identify individual countries. Instead, I focused on the top five countries so that their trends could be compared more clearly. A possible extension of this project would be to create separate visualizations by geographic region or to compare countries with similar levels of emissions, have the top three producers compared to the dsitributions of the 3 lowest. I strive to push this research to continue to theorize how to combat the detrements of such high levels of co2 emissions, when it is is directly seeping in the air from one country at large.