---
title: "Vizualization"
author: "Aud H. Halbritter"
date: "11/10/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

source("R/load_packages.R")
source("R/download_clean_data.R")

biomass <- read_csv("data/biomass/FunCaB_clean_biomass_2015-2021.csv")
community <- read_csv("data/community/FunCaB_clean_composition_2015-2019.csv")
CO2_final_1517 <- read_csv(file = "data/cflux/FunCaB_clean_Cflux_2015-2017.csv")


meta <- biomass %>% 
  distinct(siteID) %>% 
  mutate(biogeographic_zone = recode(siteID,
                             Ulvehaugen = "alpine",
                             Lavisdalen = "alpine",
                             Gudmedalen = "alpine",
                             Skjelingahaugen = "alpine",
                             Alrust = "sub.alpine",
                             Hogsete = "sub.alpine",
                             Rambera = "sub.alpine",
                             Veskre = "sub.alpine",
                             Fauske = "boreal",
                             Vikesland = "boreal",
                             Arhelleren = "boreal",
                             Ovstedalen = "boreal"),
        prep_level = recode(siteID,
                               Ulvehaugen = 1,
                               Lavisdalen = 2,
                               Gudmedalen = 3,
                               Skjelingahaugen = 4,
                               Alrust = 1,
                               Hogsete = 2,
                               Rambera = 3,
                               Veskre = 4,
                               Fauske = 1,
                               Vikesland = 2,
                               Arhelleren = 3,
                               Ovstedalen = 4),
        climate = paste0(biogeographic_zone, prep_level),
        climate = factor(climate, levels = c("boreal1", "boreal2", "boreal3", "boreal4", "sub.alpine1", "sub.alpine2", "sub.alpine3", "sub.alpine4", "alpine1", "alpine2", "alpine3", "alpine4")))

```

### Biomass

Total biomass removed in all years was `total_biomass`g


```{r biomass}

biomass_plot <-  biomass %>% 
  filter(removed_fg %in% c("F", "B", "G")) %>% 
  left_join(meta, by = c("siteID")) %>% 
   mutate(treatment = factor(treatment, levels = c("B", "F", "G", "FB", "GB", "GF", "FGB", "XC")),
          year = factor(year, levels = c("2021", "2020", "2019", "2018", "2017", "2016", "2015"))) %>% 
  ggplot(aes(x = treatment, y = biomass, fill = removed_fg)) +
  geom_col() +
  scale_fill_manual(name = "Functional group", values = c("orange", "purple", "limegreen"),
                    guide = guide_legend(direction = "vertical", 
                                         title.position = "left", 
                                         title.theme = element_text(angle = 90),
                                         #label.position="right", 
                                         label.hjust = 0.5, label.vjust = 0.5,
                                         label.theme = element_text(angle = 90))) +
  labs(x = "Removal treatment", y = "Removed functional group biomass in g") +
  facet_grid(year ~ climate) +
  theme_bw() +
  theme(axis.text.x  = element_text(angle=90, vjust=0.5),
        #axis.title.x = element_text(angle=90),
        strip.text.y.right = element_text(angle = 90))

biomass_plot

#ggsave("biomass_plot.jpeg", biomass_plot, dpi = 300, height = 8, width = 14)

```

```{r temp}
dailyTemperature <- iButtonData %>%
  filter(!is.na(soiltemperature)) %>%
  mutate(date = dmy(format(date_time, "%d.%b.%Y"))) %>%
  group_by(date, siteID, treatment) %>%
  summarise(n = n(),
            soiltemperature = mean(soiltemperature),
            max_temp = max(soiltemperature)) %>%
  # filter(n > threshold) %>%
  # select(-n, -sum)
  left_join(meta, by = c("siteID", "treatment"))


dailyTemperature %>%
  mutate(treatment = factor(treatment, levels = c("C", "B", "F", "G", "FB", "GB", "GF", "FGB")),
         biogeographic_zone = factor(biogeographic_zone, levels = c("boreal", "sub.alpine", "alpine"))) %>% 
  select(date, siteID, treatment, max_temp) %>%
  pivot_wider(names_from = treatment, values_from = max_temp) %>%
  pivot_longer(cols = -c(date, siteID, C), names_to = "treatment", values_to = "max_temp") %>%
  mutate(diff = C - max_temp) %>%
    # filter(treatment %in% c("C", "FGB"),
    #      prep_level == 4) %>%
  ggplot(aes(x = date, y = diff, colour = treatment)) +
  geom_line() +
  #scale_color_manual(values = c("limegreen", "darkgoldenrod4")) +
  facet_wrap(~ siteID) +
  theme_bw()



# data checking
soilmoisture %>%
  left_join(meta, by = ) %>% 
  mutate(climate = paste0(biogeographic_zone, prep_level),
         climate = factor(climate, levels = c("boreal1", "boreal2", "boreal3", "boreal4", "sub.alpine1", "sub.alpine2", "sub.alpine3", "sub.alpine4", "alpine1", "alpine2", "alpine3", "alpine4"))) %>%
  ggplot(aes(x = treatment, y = soilmoisture, colour = siteID)) +
  geom_point() +
  facet_grid(year(date) ~ climate)



```


``` {r community}

comm_data <- community %>%
  select(year, siteID:total_bryophytes, -turfID) %>%
  filter(treatment %in% c("C", "FB", "GB", "GF"),
         year != 2016) %>%
  #filter(blockID == "Alr5") %>%
  distinct() %>%
  mutate(total_graminoids = if_else(year == 2015 & treatment %in% c("GB", "GF"), NA_real_, total_graminoids),
         total_forbs = if_else(year == 2015 & treatment %in% c("FB", "GF"), NA_real_, total_forbs),
         total_bryophytes = if_else(year == 2015 & treatment %in% c("FB", "GB"), NA_real_, total_bryophytes)) %>%
  group_by(year, siteID, treatment) %>%
  summarise_at(c("total_graminoids", "total_forbs", "total_bryophytes"), mean, na.rm = TRUE) %>%
  pivot_longer(cols = c(total_graminoids, total_forbs, total_bryophytes), names_to = "fg", values_to = "fg_cover") %>%
  pivot_wider(names_from = treatment, values_from = fg_cover) %>%
  mutate(Forbs = GB - C,
         Graminoids = FB - C,
         Bryophytes = GF - C) %>%
  select(-C, -FB, -GB, -GF) %>%
  pivot_longer(cols = c(Forbs, Graminoids, Bryophytes), names_to = "FunctionalGroup", values_to = "Cover") %>%
  filter(!is.na(Cover)) %>%
  mutate(FunctionalGroup = factor(FunctionalGroup, levels = c("Graminoids", "Forbs", "Bryophytes")))

community_plot <- comm_data %>%
  left_join(meta %>% 
              distinct(siteID, biogeographic_zone, prep_level), by = "siteID") %>% 
  mutate(biogeographic_zone = factor(biogeographic_zone, levels = c("alpine", "sub.alpine", "boreal")),
         prec_level = as.character(prep_level),
         prec_level = recode(prep_level, "1" = "700 mm", "2" = "1400 mm", "3" = "2100 mm", "4" = "2800 mm"),
         prec_level = factor(prec_level, levels = c("700 mm", "1400 mm",  "2100 mm", "2800 mm")),
         year = as.Date(ISOdate(year, 1, 1))) %>% 
  ggplot(aes(x = year, y = Cover, colour = FunctionalGroup)) +
  geom_line() +
  geom_hline(yintercept = 0, colour = "grey40", linetype = "dashed") +
  labs(y = "∆cover (treatment - control)") +
  scale_colour_manual(name = "Functional group", values = c("limegreen", "purple", "orange")) +
  scale_x_date(date_labels="%y") +
  facet_grid(biogeographic_zone ~ prec_level) +
  theme_set(theme_bw(base_size = 18)) +
  theme(legend.position="top")

community_plot

#ggsave("community_plot.jpeg", community_plot, dpi = 300, height = 8, width = 12)

```


### Carbon Fluxes

``` {r cflux}


CO2_final_1517 %>%
  filter(year == 2017) %>%
  left_join(meta, by = c("siteID")) %>%
  mutate(treatment = factor(treatment, levels = c("C", "B", "F", "G", "FB", "GB", "GF", "FGB")),
         biogeographic_zone = factor(biogeographic_zone, levels = c("alpine", "sub.alpine", "boreal"))) %>%
  ggplot(aes(y = gpp, treatment)) +
  geom_boxplot() +
  facet_grid(biogeographic_zone ~ prep_level)



```

