DS Labs Assignment

Author

Ellis Oppong

Load Libraries

# Load needed packages
library(dslabs)
library(tidyverse)
── 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.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── 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
library(ggthemes)

Load Dataset

# Load greenhouse gases dataset
data("greenhouse_gases")

Data Cleaning

# Check structure of the dataset
str(greenhouse_gases)
'data.frame':   300 obs. of  3 variables:
 $ year         : num  20 40 60 80 100 120 140 160 180 200 ...
 $ gas          : chr  "CO2" "CO2" "CO2" "CO2" ...
 $ concentration: num  278 278 277 277 278 ...

Data Visualisation

# Create heatmap
greenhouse_gases %>%
  ggplot(aes(x = year, y = gas, fill = concentration)) +
  geom_tile() +
  scale_fill_viridis_c(option = "C") +
  labs(
    title = "Heatmap of Greenhouse Gas Concentrations",
    subtitle = "Greenhouse gas concentrations across 2000 years",
    x = "Year",
    y = "Gas Type",
    fill = "Concentration"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 16, face = "bold"),
    plot.subtitle = element_text(size = 12),
    axis.title = element_text(size = 12),
    legend.title = element_text(size = 11)
  ) 

# Assisted by chatgpt to write code for changing the font of the heading, axes labels and legend.

Graph Description

In this assignment, I used the greenhouse gases dataset from the ds labs package. This dataset looks into the historical atmospheric concentration of three greenhouse gases, i.e., carbon dioxide (CO₂), methane (CH₄), and nitrous oxide (N₂O), spanning the course of 2000 years. I created a heatmap to show how the concentrations of these gases have changed over those years. In the graph, the x-axis represents the year, the y-axis shows the gas type, and the color fill represents concentration levels. I used the geom_tile() function to create the heatmap and applied viridis color palette (“option C”) using scale_fill_viridis_c(). I changed the font sizes of the headings, axis labels, and legend in the theme_minimal() base, which enhanced the overall presentation of the graph. This heatmap shows the stability of greenhouse gas levels throughout the majority of history and a sharp increase in concentrations during recent centuries, especially after the Industrial Revolution.