knitr::opts_chunk$set(echo = TRUE)
library("tidyverse")
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.5     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library("readr")
library("lubridate")
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library("grid")
library("gridExtra")
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
options(digits = 3)

Recommended libraries installed and imported (tidyverse, readr, lubridate, grid, gridExtra).
Number of significant figures set to 3.

emissions <- read_csv("annual-co2-emissions-per-country.csv")
## Rows: 20937 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): Country, Code
## dbl (2): Year, CO2_annual_tonnes
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
class(emissions)
## [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"
emissions %>%
  mutate(CO2_daily_tonnes = CO2_annual_tonnes/365.25)
## # A tibble: 20,937 x 5
##    Country     Code   Year CO2_annual_tonnes CO2_daily_tonnes
##    <chr>       <chr> <dbl>             <dbl>            <dbl>
##  1 Afghanistan AFG    1949             14656             40.1
##  2 Afghanistan AFG    1950             84272            231. 
##  3 Afghanistan AFG    1951             91600            251. 
##  4 Afghanistan AFG    1952             91600            251. 
##  5 Afghanistan AFG    1953            106256            291. 
##  6 Afghanistan AFG    1954            106256            291. 
##  7 Afghanistan AFG    1955            153888            421. 
##  8 Afghanistan AFG    1956            183200            502. 
##  9 Afghanistan AFG    1957            293120            803. 
## 10 Afghanistan AFG    1958            329760            903. 
## # ... with 20,927 more rows
emissions %>%
  transmute(Country = Country, Year = Year, CO2_annual_tonnes = CO2_annual_tonnes, CO2_daily_tonnes = CO2_annual_tonnes/365.25) %>%
  head(6)
## # A tibble: 6 x 4
##   Country      Year CO2_annual_tonnes CO2_daily_tonnes
##   <chr>       <dbl>             <dbl>            <dbl>
## 1 Afghanistan  1949             14656             40.1
## 2 Afghanistan  1950             84272            231. 
## 3 Afghanistan  1951             91600            251. 
## 4 Afghanistan  1952             91600            251. 
## 5 Afghanistan  1953            106256            291. 
## 6 Afghanistan  1954            106256            291.

annual-co2-emissions-per-country.csv imported into new data chunk.
Data class checked to ensure in form of data frame.
Column data types match the data contained.
A new column containing the mean daily emission per country per year was added as CO2_daily_tonnes.
Transmute function used to remove the Code column of the data frame.
Piping and head functions used to print the first 6 rows of this data.

Question 1

Which country (excluding “world”) produced the most CO2 in 2005? How many tonnes of CO2 was this emission?

emissions %>%
  filter(Year ==  "2005", Country != "World") %>%
  summarise(Highest_CO2_producer_2005 = Country[which(CO2_annual_tonnes==max(CO2_annual_tonnes))])
## # A tibble: 1 x 1
##   Highest_CO2_producer_2005
##   <chr>                    
## 1 United States
emissions %>%
  filter(Year ==  "2005", Country != "World") %>%
  summarise(Highest_CO2_emissions_2005 = max(CO2_annual_tonnes))
## # A tibble: 1 x 1
##   Highest_CO2_emissions_2005
##                        <dbl>
## 1                 6131892976

The filter, summarise, and which functions were used in piping operations.
The filter function was used to only include CO2 emissions from 2005 and to remove ‘World’ values.
The which function was used to find the country with the maximum CO2 emissions value from the filtered data.
The results show the United States was the highest annual producer of CO2 in 2005, with emissions of 6.13e+09 tonnes.

Question 2

Which country produced the least CO2 in 2019? How many tonnes of CO2 was this emission?

emissions %>%
  filter(Year ==  "2019", Country != "World") %>%
  summarise(Lowest_CO2_producer_2019 = Country[which(CO2_annual_tonnes==min(CO2_annual_tonnes))])
## # A tibble: 1 x 1
##   Lowest_CO2_producer_2019
##   <chr>                   
## 1 Niue
emissions %>%
  filter(Year ==  "2019", Country != "World") %>%
  summarise(Lowest_CO2_emissions_2019 = min(CO2_annual_tonnes))
## # A tibble: 1 x 1
##   Lowest_CO2_emissions_2019
##                       <dbl>
## 1                      8154

Code altered to include only CO2 emissions from 2019 and to find the country with the lowest CO2 emissions.
The country of Niue was the lowest producer of CO2 in 2019, producing only 8154 tonnes.

Question 3

What was the mean mass of CO2 emitted by the United Kingdom between 2010 and 2015? What was the standard deviation over the same period? What percentage of the global total over the same period does this represent?

emissions %>%
  filter(Country == "United Kingdom", Year %in% (2010:2015)) %>%
  summarise(Mean_UK_CO2 = mean(CO2_annual_tonnes))
## # A tibble: 1 x 1
##   Mean_UK_CO2
##         <dbl>
## 1  468178737.
emissions %>%
  filter(Country == "United Kingdom", Year %in% (2010:2015)) %>%
  summarise(sd_UK_CO2 = sd(CO2_annual_tonnes))
## # A tibble: 1 x 1
##   sd_UK_CO2
##       <dbl>
## 1 32679735.
emissions %>%
  filter(Country == "World", Year %in% (2010:2015)) %>%
  summarise(Mean_World_CO2 = mean(CO2_annual_tonnes))
## # A tibble: 1 x 1
##   Mean_World_CO2
##            <dbl>
## 1   34590513332.
uk_mean <- emissions %>%
  filter(Country == "United Kingdom", Year %in% (2010:2015)) %>%
  summarise(Mean_UK_CO2 = mean(CO2_annual_tonnes))
uk_sd <- emissions %>%
  filter(Country == "United Kingdom", Year %in% (2010:2015)) %>%
  summarise(sd_UK_CO2 = sd(CO2_annual_tonnes))
world_mean <- emissions %>%
  filter(Country == "World", Year %in% (2010:2015)) %>%
  summarise(Mean_World_CO2 = mean(CO2_annual_tonnes))
UK_percentage_global_total <- (uk_mean/world_mean)*100
sprintf("UK percentage of global emissions between 2010 & 2015 is %f%%", UK_percentage_global_total)
## [1] "UK percentage of global emissions between 2010 & 2015 is 1.353489%"

The mean UK CO2 emissions between 2010 and 2015 were 4.682^{8} tonnes.
The standard deviation over this period was by 3.268^{7} tonnes.
The mean World CO2 emissions between 2010 and 2015 were 3.459^{10} tonnes
This value was used to calculate the Uk percentage of global emissions across this period, by dividing the UK’s emissions by the world’s emissions.
The UK percentage of global emissions between 2010 & 2015 was 1.353489%

Question 4

What was the total mass of CO2 emitted globally between (a) 1950 and 1959? (b) 2010 and 2019? How much has the global decadal output increased between these two decades, and what percentage of the 1950-1959 total emissions does this difference represent?

emissions %>%
  filter(Country == "World", Year %in% (1950:1959)) %>%
  summarise(Total_CO2_1950_to_1959 = sum(CO2_annual_tonnes))
## # A tibble: 1 x 1
##   Total_CO2_1950_to_1959
##                    <dbl>
## 1            73053937491
emissions %>%
  filter(Country == "World", Year %in% (2010:2019)) %>%
  summarise(Total_CO2_2010_to_2019 = sum(CO2_annual_tonnes))
## # A tibble: 1 x 1
##   Total_CO2_2010_to_2019
##                    <dbl>
## 1           351320940582
decade1 <- emissions %>%
  filter(Country == "World", Year %in% (1950:1959)) %>%
  summarise(Total_CO2_1950_to_1959 = sum(CO2_annual_tonnes))
decade2 <- emissions %>%
  filter(Country == "World", Year %in% (2010:2019)) %>%
  summarise(Total_CO2_2010_to_2019 = sum(CO2_annual_tonnes))

Global_decadal_output_increase = decade2 - decade1 
sprintf("Increase in decadal CO2 output from the 1950s to 2010s is by %.0f tonnes", Global_decadal_output_increase)
## [1] "Increase in decadal CO2 output from the 1950s to 2010s is by 278267003091 tonnes"
percentage_of_1950s_emissions = (Global_decadal_output_increase/decade1)*100
sprintf("The Global decadal output difference represents %0.3f%% of the 1950 to 1959 total emissions", percentage_of_1950s_emissions)
## [1] "The Global decadal output difference represents 380.906% of the 1950 to 1959 total emissions"

The total mass of CO2 emitted globally between 1950 and 1959 was 7.305^{10} tonnes.
The total mass of CO2 emitted globally between 2010 and 2019 was 3.513^{11} tonnes.
The increase in decadal CO2 output from the 1950s to 2010s is by 278267003091 tonnes.
The Global decadal output difference represents 380.906% of the 1950 to 1959 total emissions.

Question 5

Produce a scatter plot to show how global CO2 emissions (“world”) have changed between 1750 and 2019. Add text to your Markdown to describe the trend you observe.

library(ggplot2)
world_emissions <- emissions %>%
  filter(Country == "World", Year %in% (1750:2019)) 
plot1 <- ggplot(world_emissions, aes(x = Year, y = CO2_annual_tonnes)) +
geom_point(na.rm=TRUE, shape = 20, size = 2, colour = "Green") +
 labs(x = "Year", y = "Global CO2 emissions (tonnes)")
print(plot1)

The scatter plot shows the change in global CO2 emissions between 1750 and 2019.
The trend shows a very clear and exponential increase in global CO2 emissions, increasing in the mid-1800s and picking up significantly in the 1900s.

Question 6

Produce a scatter plot to compare the CO2 emissions from two countries (e.g., Denmark and Spain) between 1950 and 2019. Add text to your Markdown to describe the trends you observe.

china_emissions <- emissions %>%
  filter(Country == "China", Year %in% (1950:2019)) 
india_emissions <- emissions %>%
  filter(Country == "India", Year %in% (1950:2019)) 
plot1 <- ggplot(rbind(china_emissions, india_emissions), aes(x = Year, y = CO2_annual_tonnes, colour = Country)) +
geom_point(na.rm=TRUE, shape = 20, size = 2) +
 labs(x = "Year", y = "Global CO2 emissions (tonnes)")
print(plot1)

The scatter plot compares the changes in China’s and India’s CO2 emissions between 1950 and 2019.
The trend for both countries shows an increase in global CO2 emissions, however China shows a much greater increase in CO2 emissions over time compared to India.
India shows a trend of gradually increasing CO2 emissions, with emissions picking up in the 2000s.
China shows an exponentially increasing trend in its CO2 emissions, with irregular peaks within the scatter plot, and rapid emissions increases in the 2000s.