Assignment-6

Loading Libraries

library(dslabs)
library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggrepel)
data("temp_carbon")

Data set - variables

The temp_carbon dataset in the dslabs R package includes five main variables:

  • year: Year (CE), ranging from 1751 to 2018 (temperature data covers 1880–2018, while carbon emissions cover 1751–2014).

  • temp_anomaly: Global annual mean temperature anomaly in degrees Celsius relative to the 20th-century mean (1880–2018).

  • land_anomaly: Annual mean temperature anomaly over land in degrees Celsius relative to the 20th-century mean (1880–2018).

  • ocean_anomaly: Annual mean temperature anomaly over the ocean in degrees Celsius relative to the 20th-century mean (1880–2018).

  • carbon_emissions: Annual global carbon emissions in millions of metric tons of carbon (1751–2014)

Data - first few rows

head(temp_carbon)
  year temp_anomaly land_anomaly ocean_anomaly carbon_emissions
1 1880        -0.11        -0.48         -0.01              236
2 1881        -0.08        -0.40          0.01              243
3 1882        -0.10        -0.48          0.00              256
4 1883        -0.18        -0.66         -0.04              272
5 1884        -0.26        -0.69         -0.14              275
6 1885        -0.25        -0.56         -0.17              277

Cleaning the data - removing non available data

clean_climate_data <- temp_carbon %>%
  filter(!is.na(carbon_emissions) & !is.na(temp_anomaly))

Data Size

dim(clean_climate_data)
[1] 135   5

The plot

The goal is to draw a scatter plot and show the relationship between carbon emissions and global temperature.

ggplot(clean_climate_data, aes(x = carbon_emissions, y = temp_anomaly, color = year)) +
  geom_point(size = 2.5, alpha = 0.8) +
  geom_smooth(method = "lm", color = "red", linetype = "dashed", se = FALSE, size = 0.8) +
  geom_text_repel(
    aes(label=ifelse(year %in% c(1900,1950,1975,2000,2010),as.character(year),"")),
    color="black"
  )+
  labs(
    title = "Global Temperature Anomaly vs. Carbon Emissions",
    subtitle = "Data covers aligned years (1880–2014)",
    x = "Global Carbon Emissions (Millions of Metric Tons)",
    y = "Temperature Anomaly (°C relative to 20th Century Mean)",
    color = "Year" 
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", size = 14),
    legend.position = "right"
  )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
`geom_smooth()` using formula = 'y ~ x'

Conclusions:

1) As global emissions keep increasing, the temperature anomaly accelerates upwards.

2) The time variable - The color shift from dark blue to light blue shows that this relationship tracks with industrial increase in the last 100 years.