Assignment 6

Author

Nathan Pak

Load Libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── 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(ggplot2)
library(dslabs)

Store/Clean Dataset

data("greenhouse_gases")

greenhouse_indexed <- greenhouse_gases |>
  filter(year >= 1800) |> #for 1800-2000 time span
  group_by(gas) |> #group for calculation
  mutate(
    baseline = first(concentration),
    growth_index = (concentration / baseline) * 100 #calculating % of 1800
  ) |>
  ungroup() |>
  mutate(gas = factor(gas, levels = c("CH4", "CO2", "N2O"))) #reorder to match layers on area graph

summary(greenhouse_indexed) #cleaned dataset
      year       gas     concentration       baseline      growth_index  
 Min.   :1800   CH4:11   Min.   : 269.6   Min.   :272.6   Min.   : 98.9  
 1st Qu.:1840   CO2:11   1st Qu.: 284.0   1st Qu.:272.6   1st Qu.:101.2  
 Median :1900   N2O:11   Median : 303.4   Median :282.6   Median :105.5  
 Mean   :1900            Mean   : 533.7   Mean   :427.3   Mean   :117.1  
 3rd Qu.:1960            3rd Qu.: 765.9   3rd Qu.:726.6   3rd Qu.:115.7  
 Max.   :2000            Max.   :1703.4   Max.   :726.6   Max.   :234.4  

Plot Visualization

ggplot(greenhouse_indexed, aes(x = year, y = growth_index, fill = gas)) +
  geom_area(position = "identity", alpha = 0.5, color = "black", linewidth = 0.3) +
  scale_fill_manual(values = c("CH4" = "#FF5733", "CO2" = "#581845", "N2O" = "#FFC300")) + #3 non default colors
  labs(
    title = "Relative Growth of Greenhouse Gases (1800-2000)",
    x = "Year",
    y = "Growth Index (% of 1800)",
    fill = "Gas",
    caption = "Source: dslabs"
  ) +
  theme_minimal()

Explanation

For this assignment I chose to use the greenhouse_gases in order to create a visualization on concentration growth of greenhouse gases over time among the 3 gases in an area plot. When first doing this I realized that the difference in raw gas concentrations between the 3 were so large, I chose to instead use a growth index as a % of 1800 otherwise the entire plot would only show co2 as the other gas concentrations are minimal in comparison. This allows all 3 to be shown despite the massive difference and still accurately shows the growth over time. This led me to clean up the dataset to allow finding a growth index through adding an extra calculation to store a % change in column instead of using the raw concentrations themselves. For the plot itself I used geom_area with a line to distinct the 3 gases, then used 3 distinct colors, added appropriate titles and caption, and then set the theme to minimal. I do believe this was the best plot for this specific purpose although there could be more distinction for co2 as it is almost hidden by n2o.