---
title: "Infauna"
output: html_notebook
---

```{r}
library(readxl)
library(lubridate)
library(writexl)
library(ggplot2)
library(colorspace)
library(reshape2)
library(lawstat)
library(car)
setwd("/Users/Libby/Desktop/UVA/Research/Data Inventory/Additional Measurements/Infauna and OM")
infauna = read_excel("infauna.xlsx")

infauna$Date = as.factor(infauna$Date)

infauna$countsqrt = sqrt(infauna$Count)
infauna$AFDWsqrt = sqrt(infauna$AFDW)
infauna$AFDWlog = log10(infauna$AFDW)

model1 = aov(countsqrt~Date, data=infauna)
summary(model1)
TukeyHSD(model1)

model2 = aov(infauna$AFDWsqrt~Date, data=infauna)
summary(model2)
TukeyHSD(model2)
```
```{r}
model1_resids <- model1$residuals
qqPlot(model1_resids) 
hist(model1_resids)
#Shapiro Wilk's Test for Normality
shapiro.test(model1_resids) #p > 0.05, so points are normally distributed 
leveneTest(infauna$Count, infauna$Date) #p value > 0.05, so there is homogeneity of variation
```
```{r}
model2_resids <- model2$residuals
qqPlot(model2_resids) 
hist(model2_resids)
#Shapiro Wilk's Test for Normality
shapiro.test(model1_resids) #p > 0.05, so points are normally distributed 
leveneTest(infauna$Count, infauna$Date) #p value > 0.05, so there is homogeneity of variation
```


```{r}
library(ggplot2)
date = c(rep("Sep 2020" , 4) , rep("Jul 2021" , 4) , rep("Oct 2021" , 4))
Taxa = rep(c("worms" , "small crustaceans" , "large crustaceans", "bivalves") , 3)
count = c(23,5,1,0,16,1,0,1,8,1,0,4)
data = data.frame(date,Taxa,count)
data$date = factor(data$date,levels = c("Sep 2020", "Jul 2021", "Oct 2021"))
 
# Grouped
mypal2=c("wheat", "tan2", "sienna3", "sienna")
ggplot(data, aes(fill=Taxa, y=count, x=date)) + 
    geom_bar(position="stack", stat="identity", color="black",lwd=0.2) +
    xlab("Date") + ylab("Count") +
    scale_fill_manual(values=mypal2) +
    theme_linedraw() + theme(aspect.ratio=3/2) 
ggsave("infaunacount.png")
```
```{r}
mypal2=c("wheat", "tan2", "sienna3", "sienna")
ggplot(data, aes(fill=Taxa, y=count, x=date)) + 
    geom_bar(position="fill", stat="identity",color="black",lwd=0.2) +
    xlab("Date") + ylab("Percent") +
    scale_fill_manual(values=mypal2) +
    theme_linedraw() + theme(aspect.ratio=3/2) 
ggsave("infaunapercent.png")
```
```{r}
library(ggplot2)
date = c(rep("Sep 2020" , 4) , rep("Jul 2021" , 4) , rep("Oct 2021" , 4))
Taxa = rep(c("worms" , "small crustaceans" , "large crustaceans", "bivalves") , 3)
afdw = c(0.22,0.08,0.09,0,0.16,0.004,0,0.009,0.18,0.02,0,0.05)
data = data.frame(date,Taxa,afdw)
data$date = factor(data$date,levels = c("Sep 2020", "Jul 2021", "Oct 2021"))
 
# Grouped
mypal2=c("wheat", "tan2", "sienna3", "sienna")
ggplot(data, aes(fill=Taxa, y=afdw, x=date)) + 
    geom_bar(position="stack", stat="identity", color="black",lwd=0.2) +
    xlab("Date") + ylab("Ash free dry weight (g)") +
    scale_fill_manual(values=mypal2) +
    theme_linedraw() + theme(aspect.ratio=3/2) 
ggsave("infaunaafdw.png")
```
```{r}
mypal2=c("wheat", "tan2", "sienna3", "sienna")
ggplot(data, aes(fill=Taxa, y=afdw, x=date)) + 
    geom_bar(position="fill", stat="identity", color="black",lwd=0.2) +
    xlab("Date") + ylab("Percent") +
    scale_fill_manual(values=mypal2) +
    theme_linedraw() + theme(aspect.ratio=3/2) 
ggsave("infaunaafdwpercent.png")
```


