---
title: "Van Wert et al 2023 Fig 5"
author: "Jacey C. Van Wert"
output: html_document
date: "2023-03-28"
---

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

library(GGally)
library(here)
library(tidyverse)
library(patchwork)
```


Using: https://r-charts.com/ranking/parallel-coordinates-ggplot2/
```{r}
parallel <- read_csv(here::here("data","VanWert_etal_2023_fishfeces.csv"))%>%
  mutate(mg_ppm_100 = mg_ppm/100,
         fe_ppm_100 = fe_ppm/100,
         ca_ppm_1000 = ca_ppm/1000)

new_palette_6 <- c("#1E6A6C","#38A1A0", "#95A662","#DAAB18", "#EE7E09", "#F83B0B")
```


```{r}
parallel_100 <- parallel%>%
  dplyr::select(!mg_ppm)%>% dplyr::select(!fe_ppm)

nutrients_l_100 <- parallel_100 %>% 
  pivot_longer(ba_ppm:ca_ppm_1000, names_to = "micro", values_to = "ppm")
nutrient_sum_100  <- nutrients_l_100 %>% 
  group_by(trophic_group, micro) %>%
  dplyr::summarise(mean.value = mean(ppm, na.rm=TRUE))

nutrients_w_100 <- nutrient_sum_100 %>%
  dplyr::group_by(trophic_group)%>%
  pivot_wider(names_from = micro, values_from = mean.value)%>%
  dplyr::select(trophic_group, cu_ppm, fe_ppm_100, mg_ppm_100, ca_ppm_1000, mn_ppm, zn_ppm)%>%
  dplyr::rename(Ca1000 = ca_ppm_1000, Mg100 = mg_ppm_100, Cu = cu_ppm, Mn = mn_ppm, Zn = zn_ppm, Fe100 = fe_ppm_100)

ggparcoord(data = nutrients_w_100,
           columns = 2:7,
           groupColumn = "trophic_group",
           scale="globalminmax",
           showPoints = TRUE)+
  scale_color_manual(values = new_palette_6, "Trophic Guild") +
  theme_classic()+
  scale_x_discrete(limits = c("Ca1000", "Mg100", "Fe100", "Zn", "Mn", "Cu"), position="top")+
  geom_line(size=2, alpha = .5)+
  geom_point(size=4)+
  labs(x = "Nutrient\n", y = "Feces nutrient content (ppm)\n")+
  theme(axis.ticks.x = element_blank(),
        axis.title.y=element_text(size=18),
        axis.title.x=element_text(size=18),
        strip.text = element_text(size = 18), 
        plot.title = element_text(hjust = 0.5,size=20),
        legend.title = element_text(size = 18),
        legend.text = element_text(size = 18),
        axis.text.y=element_text(size=18, colour= 'black'),
        axis.text.x=element_text(size=18, colour= 'black'),
        axis.line.y=element_line(colour = 'black',size=0.5),
        axis.line.x=element_line(colour = 'black',size=0.5))

ggsave(here::here("fig5.png"), height = 5, width = 10)
```


