The Keeling Curve in R

Rajesh Sigdel

2021-05-11

This document shows how to make the Keeling curve using carbon dioxide concentration dataset obtained from the National Oceanic and Atmospheric Administration website.

(https://www.esrl.noaa.gov/gmd/webdata/ccgg/trends/co2/co2_mm_mlo.txt) (Click CTRL+Enter)

Downloading dataset

my_data <- read.table("ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_mm_mlo.txt", 
                      comment.char = '#', header = F,   
                      col.names= c('Year','Month','Time','Co2_Concentration',
                                   'Interpolated','Trend',
                        'Days', 'ucmean'))
## Warning in read.table("ftp://aftp.cmdl.noaa.gov/products/trends/co2/
## co2_mm_mlo.txt", : incomplete final line found by readTableHeader on 'ftp://
## aftp.cmdl.noaa.gov/products/trends/co2/co2_mm_mlo.txt'

Packages

We need to install tidyverse and lubridate if we dont have them installed in our computer.

# install.packages ("tidyverse")
library (tidyverse)
## -- Attaching packages --------------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
# install.packages ("lubridate")
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union

You can also download the same dataset from the link: https://www.esrl.noaa.gov/gmd/webdata/ccgg/trends/co2/co2_mm_mlo.txt (Click CTRL+Enter) I prefer to manually download the dataset from the website and store it into my computer.

The following code reads the dataset into R and changes the column names so that we can easily identify the required column later on.

data <- read.table('data.txt', sep = "",header = F)

my_cc <- data %>% 
  rename(Year= V1, Month = V2, Time = V3,
         Co2_Concentration = V4, Interpolated = V5, 
         Trend = V6, Days = 7, ucmean = V8)

data_cc <- my_cc %>% 
  mutate(
    Co2_Con = case_when(
      Co2_Concentration == -99.99 ~ Interpolated,
      TRUE ~ Co2_Concentration
    )
  )

Separating datetime column and averaging the averaging each months carbon concentration

data_cc$Date <- ymd(paste(data_cc$Year, " ", data_cc$Month, " ", "15"))
data_cc_sel <- data_cc %>% 
  select(Year, Month, Date, Co2_Con )

Finally the keeling curve

We will use ggplot2, a popular package for data visualization, to create keeling curve

ggplot(data_cc_sel,aes(Date, Co2_Con)) +
  geom_line(color='red') +
  xlab("Year, Month") +
  scale_x_date(date_labels = "%Y-%m", date_breaks = "5 year") +
  theme(axis.text.x = element_text(face = "bold", color = "#993333", 
                                   size = 12, angle = 45, hjust = 1)) +
  ylab("CO2 Concentration (ppm)") +
  #scale_x_continuous(breaks = trans_breaks(identity, identity, n = 10))
  scale_y_continuous() +
  theme(axis.text.y = element_text(face = "bold", color = "#993333", 
                                   size = 10, hjust = 1),axis.title.y = element_text(size = 10))+
  theme_bw()

The curve shows the annual increase in Carbon dioxide gas in the atmosphere. The curve also captures the seasonal variation (changes) in the atmospheric carbon dioxide gas.