Untitled

 avatar
unknown
plain_text
4 years ago
2.4 kB
4
Indexable
#Library input

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

#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("CH4_13m6_2012til2021.csv") %>% 
  mutate(
    date = ymd(paste(Year, Month, Day, sep= ' ')),
    datetime = ymd_hm(paste(Year, Month, Day, Hour, Minute, sep= ' '))
  )

data$HYY_META.CH4168 <- as.numeric(data$HYY_META.CH4168)



#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(
    cH4.avg = mean(HYY_META.CH4168, na.rm = TRUE),
    cH4.min = min(HYY_META.CH4168, na.rm = TRUE),
    cH4.max = max(HYY_META.CH4168, na.rm = TRUE),
    cH4.q25 = quantile(HYY_META.CH4168, 0.25, na.rm = TRUE),
    cH4.q75 = quantile(HYY_META.CH4168, 0.75, na.rm = TRUE)
  )

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)
  ) 

data.plot4 %>% 
  ggplot(aes(x=yearmonth, y=cH4.avg)) +
  geom_ribbon(aes(ymin=cH4.min, ymax=cH4.max, fill = "Min & Max values"),  alpha=0.4) +
  geom_ribbon(aes(ymin=cH4.q25, ymax=cH4.q75, fill = "Quantiles"), alpha=0.6) +
  geom_line(aes(y=cH4.avg, color = "Average"), alpha=0.6) +
  labs(
    title = "CH4 Concentration, 2012-2021",
    x = "Year",
    y = "CH4, ppm"
  ) +
  scale_color_manual(name="", values = colors) +
  scale_fill_manual(name="", values = fills) +
  scale_y_continuous(breaks = seq(1.8, 2.2, by= 0.1), limits = c(1.8, 2.2)) + 
  scale_x_date(date_breaks = '2 year', expand = c(0,0), labels = date_format("%Y")) +
  theme.eyrun 




Editor is loading...