---
title: "Recruitment"
output: html_notebook
---

```{r}
library(readxl)
library(lubridate)
library(writexl)
library(ggplot2)
library(colorspace)
library(reshape2)
library(psych)
library(car)
library(onewaytests)
library(PMCMRplus)
library(lawstat)
setwd("/Users/Libby/Desktop/UVA/Research/Data Inventory/Additional Measurements/Recruitment Tiles")
Count = read_excel("Recruitment.xlsx",sheet="count")
Length = read_excel("Recruitment.xlsx",sheet="shell length")
```

```{r}
hist(Count$Count)
hist(Length$Length)
```

```{r}
levene.test(Count$Count, Count$Substrate) #p value > 0.05, so there is homogeneity of variation

anova1 = aov(Count ~ Substrate, data = Count)
summary(anova1)

anova1_resids = anova1$residuals

shapiro.test(anova1_resids)
#p>0.05, fail to reject null, points are normally distributed 
```

```{r}
t.test(Count~Substrate,data=Count,alternative="two.sided",var.equal=TRUE)
#p-value = 0.2573, no significant difference between counts on each substrate 
```
```{r}
mypal=c("blue3", "red1")
ggplot(data = Length, aes(x=Substrate, y = Length, fill = Substrate)) +
  geom_boxplot() +
  ylab("Shell length (mm) (per 10.2 cm2)") +
  theme_light() + 
  theme(aspect.ratio=1/1.5) +
  scale_fill_manual(values=mypal) 
ggsave("boxplot1.png")
```
```{r}
levene.test(Length$Length, Length$Substrate) #p value > 0.05, so there is homogeneity of variation

anova2 = aov(Length ~ Substrate, data = Length)
summary(anova2)

anova2_resids = anova2$residuals

hist(anova2_resids)

shapiro.test(anova2_resids)
#p<0.05, reject null, points are NOT normally distributed
```
```{r}
t.test(Length~Substrate,data=Length,alternative="two.sided",var.equal=TRUE)
#p-value > 0.05, no significant difference between lengths on each substrate 
```
```{r}
describeBy(Count$Count, group = Count$Substrate)
```
```{r}
describeBy(Length$Length, group = Length$Substrate)
```

```{r}
#calculate 95% confidence intervals
SandbarCountCI = 68.5 + c(-12.706,12.706)*0.5
CastlesCountCI = 52.8 + c(-2.262,2.262)*5.62

SandbarLengthCI = 14.22 + c(-1.98,1.98)*0.49
CastlesLengthCI = 13.66 + c(-1.96,1.96)*0.68
```

```{r}
#First create vectors for your grouping variable, dependent variable, and 95% CIs
#independent variable
group = c("oyster castles", "Sandbar")
#depependent variable 
yvar = c(52.8, 68.5)
#95% ci of means for each group
ci = c(12.7, 6.4)

#Combine into data frame
errorbars = data.frame(group, yvar, ci)

#create barplot w/error bars
```

```{r}
mypal=c("red1","blue3")
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("Counts") +
  xlab("Substrate") + 
  theme(aspect.ratio=3/2) 
ggsave("CountsBar.png")
```
```{r}
#First create vectors for your grouping variable, dependent variable, and 95% CIs
#independent variable
group = c("oyster castles", "Sandbar")
#depependent variable 
yvar = c(13.6, 14.2)
#95% ci of means for each group
ci = c(1.4, 1)

#Combine into data frame
errorbars = data.frame(group, yvar, ci)

#create barplot w/error bars
```

```{r}
mypal=c("red1","blue3")
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("Shell Length (mm) (per 10.2 cm2)") +
  xlab("Substrate") + 
  theme(aspect.ratio=3/2) 
ggsave("LengthsBar.png")
```




