---
title: "erosion pins"
output: html_notebook
---

```{r}
library(readxl)
library(lubridate)
library(writexl)
library(ggplot2)
library(colorspace)
library(reshape2)
library(psych)
library(car)

setwd("/Users/Libby.bieri/Box/Libby/Oysters/NFWF Wachapreague/Additional Measurements/Erosion Pins")

erosionpins = read_excel("erosionpins.xlsx")
describeBy(erosionpins$June, group = erosionpins$category)
```


```{r}
erosionpins$distancesqrt = sqrt(erosionpins$June)
pins.anova<- aov(erosionpins$distancesqrt ~ category, data = erosionpins)
summary(pins.anova) 
```

```{r}
pins.anova_resids <- pins.anova$residuals
qqPlot(pins.anova_resids) 
hist(pins.anova_resids)
#Shapiro Wilk's Test for Normality
shapiro.test(pins.anova_resids) #p > 0.05, so points are normally distributed 
leveneTest(erosionpins$distance, erosionpins$category) #p value > 0.05, so there is homogeneity of variation
```
```{r}
mypal=c("palegreen3", "grey")
ggplot(erosionpins,aes(x=category,y=June, fill=category)) + geom_boxplot(show.legend = FALSE) +
  scale_fill_manual(values=mypal) +
  ylab("Distance eroded (cm)") + 
  xlab("") + 
  theme_light() +
  theme(aspect.ratio=2/3)
ggsave("boxplot.png")
```
```{r}
#calculate 95% confidence intervals
noci = 124 + c(-2.306,2.306)*17.45
reefci = 53 + c(-2.179,2.179)*13.3
```

```{r}
#First create vectors for your grouping variable, dependent variable, and 95% CIs
#independent variable
group = c("no reef", "reef")
#depependent variable 
yvar = c(124, 53)
#95% ci of means for each group
ci = c(37.98,28.95)

#combine into data fram
errorbars = data.frame(group, yvar, ci)
```

```{r}
#create barplot w/error bars
mypal=c("palegreen3","grey")
ggplot(errorbars, aes(x=group, y=yvar,fill=group)) +
  geom_bar(stat="identity", width = 0.3, color="black", lwd=0.3, show.legend = FALSE)+
  geom_errorbar(aes(ymin=yvar-ci, ymax=yvar+ci), width=0.1, color="black") +
  theme_light() +
  scale_fill_manual(values=mypal) +
  ylab("Average distance eroded (cm)") +
  xlab("") + 
  theme(aspect.ratio=2/3) 
ggsave("barchart.png")
```
```{r}
S4 = read_excel("exposedpins.xlsx", sheet= "S4")
describeBy(S4$Distance, group = S4$Time) #S4 pre and post construction erosion (cm)

S7 = read_excel("exposedpins.xlsx", sheet= "S7")
describeBy(S7$Distance, group =S7$Time) #S7 pre and post construction distances eroded (cm)
```
```{r}
#comparing erosion at S4 pre and post construction
S4$distancesqrt = sqrt(S4$Distance)
S4.anova<- aov(S4$distancesqrt ~ Time, data = S4)
summary(S4.anova) # p > 0.05, so there is no significant difference in erosion pre and post const at S4
```

```{r}
S4.anova_resids <- S4.anova$residuals
qqPlot(S4.anova_resids) 
hist(S4.anova_resids)
#Shapiro Wilk's Test for Normality
shapiro.test(S4.anova_resids) #p > 0.05, so points are normally distributed 
leveneTest(S4$Distance, S4$Time) #p value > 0.05, so there is homogeneity of variation
```
```{r}
#comparing erosion at S7 pre and post construction
S7$distancesqrt = sqrt(S7$Distance)
S7.anova<- aov(S7$distancesqrt ~ Time, data = S7)
summary(S7.anova) # p < 0.05, so there is a significant difference in erosion pre and post construction at S7
```

```{r}
S7.anova_resids <- S7.anova$residuals
qqPlot(S7.anova_resids) 
hist(S7.anova_resids)
#Shapiro Wilk's Test for Normality
shapiro.test(S7.anova_resids) #p > 0.05, so points are normally distributed 
leveneTest(S7$Distance, S7$Time) #p value > 0.05, so there is homogeneity of variation
```

```{r}
allpolesnoreef = read_excel("exposedpins.xlsx", sheet= "erosion pins no reef")
describeBy(allpolesnoreef$`Distance (cm)`, group =allpolesnoreef$Time)
```

```{r}
#comparing erosion at all erosion pins at locations without reefs pre and post construction
#pre constructoin is aerial imagery analysis 1994-2020 and post construction is erosoin pin measurements 2021-2022
allpolesnoreef$distancesqrt = sqrt(allpolesnoreef$`Distance (cm)`)
allpolesnoreef.anova<- aov(allpolesnoreef$`Distance (cm)` ~ Time, data = allpolesnoreef)
summary(allpolesnoreef.anova) # p > 0.05, so there is no significant difference in erosion pre and post construction at erosoin pin locations without reefs 
```

```{r}
allpolesnoreef.anova_resids <- allpolesnoreef.anova$residuals
qqPlot(allpolesnoreef.anova_resids) 
hist(allpolesnoreef.anova_resids)
#Shapiro Wilk's Test for Normality
shapiro.test(allpolesnoreef.anova_resids) #p > 0.05, so points are normally distributed 
leveneTest(allpolesnoreef$`Distance (cm)`, allpolesnoreef$Time) #p value > 0.05, so there is homogeneity of variation
```
```{r}
S4S7 = data.frame(mean=c(29,6.8, 61,116), sd = c(14.7,9.6, 36,80), Time=as.factor(c("Post", "Post","Pre","Pre")),Section=as.factor(c("S4","S7", "S4", "S7")))

ggplot(df, aes(x=Time, y=mean, fill=Section)) +
  geom_bar(position="dodge", stat="identity", color="black")+
  theme_light() +
  ylab("Mean retreat rate (cm/yr)") +
  xlab("") + 
  theme(aspect.ratio=3/2) +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.1, position=position_dodge(0.9)) +
  scale_fill_manual(values=mypal) 
```


