Untitled

 avatar
unknown
markdown
3 years ago
2.7 kB
5
Indexable

title: "CO2-Timeseries" author: "Eyrún" date: "10/22/2021" output: html_document

knitr::opts_chunk$set(echo = TRUE)

Library input

The libraries I am using

library(tidyverse)
library(ggplot2)
library(lubridate)

Data Input

Read the CSV and then we make a date and datetime columns.

I actually don't use the datetime column but it's there if we change our minds.

data <- read_csv("CO2_13m6_2009til2021.csv") %>% 
  mutate(
    date = ymd(paste(Year, Month, Day, sep= ' ')),
    datetime = ymd_hm(paste(Year, Month, Day, Hour, Minute, sep= ' '))
  )

Plot 1: Daily averages

Let's look at this from a daily averages point of view.

data.plot1 <- data %>% 
  group_by(date) %>% 
  summarise(
    co2.avg = mean(HYY_META.CO2168, na.rm = TRUE)
  ) 

data.plot1 %>% 
  ggplot(aes(x=date, y=co2.avg)) +
    geom_line()

Plot 2: monthly averages

Plot 1 was a bit loud so let's try monthly averages.

data.plot2 <- data %>% 
  mutate(
    yearmonth = floor_date(date, "month")
  ) %>% 
  group_by(yearmonth) %>% 
  summarise(
    co2.avg = mean(HYY_META.CO2168, na.rm = TRUE)
  ) 

data.plot2 %>% 
  ggplot(aes(x=yearmonth, y=co2.avg)) +
    geom_point() +
    geom_line()

Plot 3: All together now.

We merge together the data for daily and monthly average to a single ugly plot.

data.plot2 %>% 
  ggplot(aes(x=yearmonth, y=co2.avg)) +
    geom_line(data=data.plot1, aes(x=date, y=co2.avg), col="green", alpha=0.4) +
    geom_line() + 
    geom_point()

Plot 4

Here we take the month average as well as the min, max and quantiles.

We add the extra data as a ribbon in the background of the plot. The quantile ribbon seems a bit useless so you might wish to take that away.

Remember to label what the hell the ribbons are in the final chart.

data.plot4 <- data %>% 
  mutate(
    yearmonth = floor_date(date, "month")
  ) %>% 
  group_by(yearmonth) %>% 
  summarise(
    co2.avg = mean(HYY_META.CO2168, na.rm = TRUE),
    co2.min = min(HYY_META.CO2168, na.rm = TRUE),
    co2.max = max(HYY_META.CO2168, na.rm = TRUE),
    co2.q25 = quantile(HYY_META.CO2168, 0.25, na.rm = TRUE),
    co2.q75 = quantile(HYY_META.CO2168, 0.75, na.rm = TRUE)
  )

data.plot4 %>% 
  ggplot(aes(x=yearmonth, y=co2.avg)) +
    geom_ribbon(aes(ymin=co2.min, ymax=co2.max), fill="lightblue", alpha=0.4) +
    geom_ribbon(aes(ymin=co2.q25, ymax=co2.q75), fill="lightgreen", alpha=0.6) +
    geom_point() +
    geom_line()

Plot 5

We can explore the boxplots for the years


data %>% 
  ggplot(aes(x=Year, y=HYY_META.CO2168, group=Year)) +
    geom_boxplot()