---
title: "Pengu R Final"
output: html_document
date: "2024-10-22"
---
Loading in the data from the Raw Data csv file
```{r}
PenguData <- read.csv("RawDataR.csv", header=TRUE, as.is = FALSE)
library(ggplot2)
library(dplyr)
library(car)
```
#________________________________Data Checking________________________
Checking the distribution of the pix.mm data and the variation in it. Results show skewed right. Maybe try log transforming data
```{r}
par(mfrow=c(1,2))
hist(PenguData$pix.mm)
boxplot(PenguData$pix.mm)
```
log transformed data looks much better
```{r}
hist(log(PenguData$pix.mm))
boxplot(log(PenguData$pix.mm))
```
creating a new column with log values
```{r}
PenguData$log_pix.mm <- log(PenguData$pix.mm)
```
#_______________________________Summary Statistics___________________
create summary stats of data and run the Block ANOVA
```{r}
PenguS <- PenguData %>% group_by(Distance, Angle, psu) %>% mutate(meanval = mean(log_pix.mm), sdval = sd(log_pix.mm), n = 3, se = sdval/sqrt(n))
Block_ANOVA <- aov(meanval ~ Angle*psu + Distance, data = PenguS)
Anova(Block_ANOVA)
options(max.print=1000000)
TukeyHSD(Block_ANOVA)
```
#_____________________Salinity Plot_______________________________
```{r}
PenguS$Angle <- as.character(PenguS$Angle)
PenguS$Angle <- gsub("Middle", "Center", PenguS$Angle)
PenguME <- PenguS[,1:5]
```
#__________________________Create Plots_________________________
Graph of mean correction factor against psu. Colored by distance, shape by angle
```{r}
PenguMES <- PenguME %>% group_by(Distance, Angle, psu) %>% summarise(meanval = mean(log_pix.mm), sdval = sd(log_pix.mm), n = 3, se = sdval/sqrt(n))
library(forcats)
PenguMES$psu <- factor(PenguMES$psu, levels = c("Air", "Fresh", "5", "10", "15", "20", "25", "30", "35"))
ggplot(PenguMES, aes(x = psu, y = meanval, group = Distance, color = factor(Distance), shape = Angle)) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = meanval - sdval, ymax = meanval + sdval), width = 0.2) +
theme_minimal() +
labs(x = "psu", y = "Mean correction factor (pixel:mm)", color = "Distance")
```
change line colors on all graphs
```{r}
line_colors <- c("black", "grey50")
```
#_____________Air data_________
Creating a new data frame to make Air graph
```{r}
AirTrial <- PenguME %>% filter(psu == "Air")
```
Creating summaries for air graph
```{r}
AirS <- AirTrial %>% group_by(Distance, Angle) %>% summarise(meanval = mean(log_pix.mm), sdval = sd(log_pix.mm), n = 3, se = sdval/sqrt(n))
AirS$Angle <- as.factor(AirS$Angle)
```
Air Graph
```{r}
Air <- ggplot(AirS, aes(x = log(Distance), y = meanval)) +
geom_point(aes(color = factor(Angle))) +
geom_smooth(method = "lm",aes(color = factor(Angle))) +
scale_color_manual(values = line_colors) +
theme_minimal() +
labs(x = "log Distance (cm)", y = "log Correction Factor (pixel:mm)", color = "Angle", title = "A")
Air
```
Creating a new dataframe for water graph
```{r}
WaterTrial <- PenguME %>% filter(psu != "Air")
```
Creating summary stats for water graph
```{r}
WaterS <- WaterTrial %>% group_by(Distance, Angle) %>% summarise(meanval = mean(log_pix.mm), sdval = sd(log_pix.mm), n = 3, se = sdval/sqrt(n))
```
Water Graph
```{r}
water <- ggplot(WaterS, aes(x = log(Distance), y = meanval)) +
geom_point(aes(color = factor(Angle))) +
geom_smooth(method = "lm",aes(color = factor(Angle))) +
scale_color_manual(values = line_colors) +
theme_minimal() +
labs(x = "log Distance (cm)", y = "log Correction Factor (pixel:mm)", color = "Angle", title = "B")
water
```
#________________________Equations for linear models___________
Air Center Equation
```{r}
centerA <- AirS %>% filter(Angle == "Center")
modelca <- lm(meanval ~ log(Distance), data = centerA)
summary(modelca)
```
Air Edge Equation
```{r}
edgeA <- AirS %>% filter(Angle == "Edge")
modelea <- lm(meanval ~ log(Distance), data = edgeA)
summary(modelea)
```
Water Center Equation
```{r}
centerW <- WaterS %>% filter(Angle == "Center")
modelcw <- lm(meanval ~ log(Distance), data = centerW)
summary(modelcw)
```
Water Edge Equation
```{r}
edgeW <- WaterS %>% filter(Angle == "Edge")
modelew <- lm(meanval ~ log(Distance), data = edgeW)
summary(modelew)
```
#______________________Combining Graphs for paper______________
Combining the water and air graphs so they sit in one figure for easier presentation
```{r}
library(gridExtra)
library(grid)
library(gridtext)
legend <- cowplot::get_legend(Air)
xaxis <- textGrob(expression(paste("log Distance (cm)")),
gp = gpar(fontsize = 10))
yaxis <- textGrob(expression(paste("log Correction Factor (pixel:mm)")), rot = 90, gp = gpar(fontsize = 10))
grid.arrange(
Air+ theme(legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank()),
water+ theme(legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank()),ncol=1, nrow = 2,
right = legend, left = yaxis, bottom = xaxis)
```