Legönd

Mjá
mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.2 kB
2
Indexable
Never
#Library input

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

library(tidyverse)
library(ggplot2)
library(lubridate)
library(zoo)
library(dplyr)
library(openair)
library(matrixStats)
library(zoo)
library(matrixStats)
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)
  )


data.plot4.theme = theme_classic()  + theme(
  axis.text=element_text(size=20),
  plot.margin = unit(c(1,1,1,1), "lines"),
  text=element_text(size=30),
  axis.text.x = element_text(size = 20), 
  axis.text.y = element_text(size = 20),
  plot.title = element_text(size=30),
  axis.title.x = element_text(size=24),
  axis.title.y = element_text(size=24)
)

#hér er ég e-ð að reyna að gera eins og í hinu plottinu, en er ekki alveg að virka
colors <- c(
  "Quantiles" = "lightgreen", 
  "Min & Max values" = "lightblue",
  "Average" = "black")

data.plot4 %>% 
  ggplot(aes(x=yearmonth, y=cH4.avg)) +
  geom_ribbon(aes(ymin=cH4.min, ymax=cH4.max), fill="lightblue", alpha=0.4) +
  geom_ribbon(aes(ymin=cH4.q25, ymax=cH4.q75), fill="lightgreen", alpha=0.6) +
  geom_line() +
  theme_classic() +
  ylab("CH4, ppm") +
  xlab("Year") +
  theme_classic()+
  theme(plot.title = element_text(size=30),
        axis.title.x = element_text(size=24),
        axis.title.y = element_text(size=24),
        axis.text.x = element_text(size=20),
        axis.text.y= element_text(size=20)
  ) +
  ylab("CH4, ppm") +
  xlab("Year") +
  scale_color_manual(name="", values = colors) +
  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")) +
  data.plot4.theme+
  ggtitle("CH4 Concentration, 2012-2021") 




#Hér fyrir neðan er e-ð sem ég er bara að prófa mig áfram í, hheld það sé flest hér að ofan sem ætti að meika sens. 

# legend

data.plot4.theme = theme_classic()  + theme(
  axis.text=element_text(size=20),
  plot.margin = unit(c(1,1,1,1), "lines"),
  text=element_text(size=30),
  axis.text.x = element_text(size = 20), 
  axis.text.y = element_text(size = 20),
  plot.title = element_text(size=30),
  axis.title.x = element_text(size=24),
  axis.title.y = element_text(size=24)
)

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

ggplot(data = data.plot4, aes(x=CO2_averages)) +
  geom_ribbon(aes(ymin=sd_minus, ymax=sd_plus, color="Std dev 2012-2021"), fill="gray", color="azure3", alpha=1) +
  geom_line(aes(y = rowmeanCH4, color="Daily Average 2019-2020"), size = 1.5, alpha = 0.8) +
  geom_line(aes(y = rowmeanCH4_all, color="Daily Average 2012-2021"), size = 1, alpha = 0.8) +
  labs(
    title = "CH4 concentrations, PPM",
    y=expression("CH"[4]),
    x=expression("Month")
  ) + 
  scale_color_manual(name="", values = colors) +
  scale_y_continuous(breaks = seq(1.85, 2.05, by= 0.05), limits = c(1.85, 2.05)) + 
  scale_x_date(date_breaks = '1 month',expand = c(0,0),labels = date_format("%b")) +
  CH4_averages.theme