Untitled

 avatar
unknown
plain_text
3 years ago
2.1 kB
5
Indexable

#install.packages("tidyverse")
#install.packages("lubridate")

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.


co2data <- read_csv("CO2_13m6_2009til2021.csv") %>% 
  mutate(
    date = ymd(paste(Year, Month, Day, sep= ' ')),
    datetime = ymd_hm(paste(Year, Month, Day, Hour, Minute, sep= ' ')),
    HYY_META.CH4168 <- as.numeric(HYY_META.CO2168)
  )

colors <- c(
  "Average" = "black"
)
fills <- c(
  "Quantiles" = "lightgreen", 
  "Min & Max values" = "lightblue"
)

theme.eyrun <- 
  theme_classic() + 
  theme(
    legend.title = element_blank(),
    legend.spacing.y = unit(-0.1, 'cm'),
    plot.title = element_text(size=30),
    axis.title.x = element_text(size=24),
    plot.margin = unit(c(1,1,1,1), "lines"),
    axis.title.y = element_text(size=24),
    axis.text.x = element_text(size=20),
    axis.text.y= element_text(size=20)
  ) 

co2data.plot <- co2data %>% 
  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)
  )

co2data.plot %>% 
  ggplot(aes(x=yearmonth, y=co2.avg)) +
  geom_ribbon(aes(ymin=co2.min, ymax=co2.max, fill = "Min & Max values"),  alpha=0.4) +
  geom_ribbon(aes(ymin=co2.q25, ymax=co2.q75, fill = "Quantiles"), alpha=0.6) +
  geom_line(aes(y=co2.avg, color = "Average"), alpha=0.6) +
  labs(
    title = "CO2 Concentration, 2012-2021",
    x = "Year",
    y = "CO2, ppm"
  ) +
  scale_color_manual(name="", values = colors) +
  scale_fill_manual(name="", values = fills) +
  scale_y_continuous(breaks = seq(360, 450, by= 10), limits = c(360, 450)) + 
  scale_x_date(date_breaks = '2 year', expand = c(0,0), labels = date_format("%Y")) +
  theme.eyrun 
Editor is loading...