---
title: "Supplemental_Figure13"
author: "Meredith Hanlon"
date: "2023-08-26"
output: html_document
---

```{r global_options, echo = FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE, echo=FALSE, error = TRUE)
```

```{r}
library(tidyverse)
library(rstatix)
library(ggpubr)
library(readxl)
library(stats)
library(viridis)
library(patchwork)
library(ggpmisc)
library(ggthemes)
library(ggrepel)
library(FactoMineR)
library(factoextra)
library(corrplot)
library(mclust)
library(broom)
library(RColorBrewer)
library(ggh4x)

stderror <- function(x) sd(x, na.rm = TRUE) / sqrt(length(na.omit(x)))

theme_mh <- function(){ 
    font <- "sans"   #assign font family up front
    theme(
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      panel.background = element_blank(),
      panel.border = element_rect(color = "black", fill = NA),
      axis.ticks = element_line(color = "black"),   
      axis.line = element_line(color = "black"),
      strip.background = element_blank(),
      legend.key = element_rect(fill = NA),
      text = element_text(family = font, size = 10, color = "black"),
      axis.text = element_text(family = font, size = 8, color = "black")
    )
}
```



Cores taken from all locations and ICP'd. Extraction with DTPA. 

````{r setup}
library(dplyr)
library(tidyr)
library(ggplot2)
library(readxl)
library(ggthemes)

icp <- read_excel("Hanlon_2023_data.xlsx", sheet = "SFigure_13")

icp2 <- icp %>% filter(`Samp Number` > 9) %>% filter(`Samp Number` !=48) %>% dplyr::select(`Sample ID`, Elem, `Conc (Calib)`, Location, Core, Depth)

names(icp2) <- c("SampleID", "Element", "ppm","Location", "Core", "Depth")


````


## Plot elements

```{r elem, fig.width = 7.5, fig.height = 10}
icp3 <- icp2 %>% 
  mutate(Depth2 = as.numeric(Depth)) %>% 
  mutate(Depth3 = case_when(Location == "CO" ~ Depth2 * 20,
                            TRUE ~ Depth2 * 10)) %>% 
  mutate(Depth = as.factor(Depth3))

icp3 <- icp3 %>% unite("BothLabels", Element, Location, sep = ".", remove = FALSE)

RmType <- function(x) {
  gsub("\\..*$", "", x)
}

p6 <- ggplot(icp3) +
  geom_boxplot(aes(x = Depth, y = ppm, fill = Location)) +
  facet_wrap2(~BothLabels,
             scales = "free_x",
             labeller=as_labeller(function(x) gsub("\\..*$", "", x)),
             ncol = 8) +
  theme_mh() + 
  scale_fill_gdocs() +
  coord_flip() +
  scale_x_discrete(limits = rev) +
  theme(legend.position = "top",        
        legend.margin=margin(0,0,0,0),
        legend.box.margin=margin(-10,-10,-10,-10))
p6 

ggsave("Supplemental_Figure_13.png", width = 7.5, height = 9, units = "in")
```

```{r elem, fig.width = 7.5, fig.height = 10}
icp3 <- icp2 %>% 
  mutate(Depth2 = as.numeric(Depth)) %>% 
  mutate(Depth3 = case_when(Location == "CO" ~ Depth2 * 20,
                            TRUE ~ Depth2 * 10)) %>% 
  mutate(Depth = as.factor(Depth3))

p6 <- ggplot(icp3) +
  geom_boxplot(aes(x = Depth, y = ppm, fill = Location)) +
  ggh4x::facet_nested_wrap(~ Element, scales = "free", ncol = 4)+ 
  theme_mh() + 
  scale_fill_gdocs() +
  coord_flip() +
  scale_x_discrete(limits = rev) +
  theme(legend.position = "top")
p6 
```
