# loading packages
library(ggpubr)
library(ggplot2)
library(plyr)
library(gridExtra)
library(wesanderson)
library(agricolae)
library(emmeans)
library(car)
library(multcomp)
# Define a function to summarizes data.
# Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
conf.interval=.95, .drop=TRUE) {
library(plyr)
# New version of length which can handle NA's: if na.rm==T, don't count them
length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else       length(x)
}
# This does the summary. For each group's data frame, return a vector with
# N, mean, and sd
datac <- ddply(data, groupvars, .drop=.drop,
.fun = function(xx, col) {
c(N    = length2(xx[[col]], na.rm=na.rm),
mean = mean   (xx[[col]], na.rm=na.rm),
sd   = sd     (xx[[col]], na.rm=na.rm)
)
},
measurevar
)
# Rename the "mean" column
datac <- rename(datac, c("mean" = measurevar))
datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
datac$ci <- datac$se * ciMult
return(datac)
}
# loading data
lumino = read.csv('alldata.csv', header = T)
lumino = subset(lumino, Depth != '0' & Depth != '0.5' & Depth != '4'& Depth != '5')
lumino$Depth = as.numeric(lumino$Depth)
lumino_freq = subset(lumino, Setting != 'monitoring')
lumino_mntr = subset(lumino, Setting == 'monitoring')
lumino_freq$bioturbated_area = (lumino_freq$area_adjusted_perc)/100 * 35.23865
# for 2 frequencies
freq_summ = summarySE(lumino_freq, measurevar="bioturbated_area", groupvars=c('Setting',"Depth"), na.rm=T)
pd <- position_dodge(0.1) # move points 0.1 to the left and right
## plotting depth profile
plot_lumifreq = ggplot(freq_summ, aes(x=Depth, y=bioturbated_area, colour=Setting, group=Setting)) +
geom_errorbar(aes(ymin=bioturbated_area-se, ymax=bioturbated_area+se), colour="black", width=.1, position=pd)+
geom_line(position=pd,lwd=1.5) +
geom_point(position=pd, size=3, shape=16) + # 16 is closed circle
xlab("Depth (cm)") +
ylab(expression(bold("Depth-specific bioturbated area " ~ (cm^2)))) +
scale_y_continuous(position = "right") +
scale_x_reverse(breaks = c(1,1.5,2,2.5,3))+
coord_flip(xlim=c(3.1, 0.9), ylim=c(0,8))+
scale_colour_manual(name="Treatments",
breaks=c("control", "high", 'low'),
labels = c("Control ambient temperature",
"3-day cycle heatwaves",
"6-day cycle heatwaves"),
values=c('#3399FF', '#FF9900', '#FF0000')) +
theme(
plot.title = element_blank(),
panel.background = element_blank(),
legend.title = element_text(size=18, face="bold", color = "black"),
legend.text = element_text(size=18, face="bold"),
legend.key.size = unit(0.8, 'inch'),
legend.key.height = unit(0.2, 'inch'),
legend.position=c(0.7, 0.15),
legend.background = element_rect(fill=alpha('white', 0.8),
size=0.5, linetype="solid",
colour ="NA"),
legend.key = element_blank(),
axis.ticks = element_line(size = 2),
axis.ticks.length = unit(0.08, "inch"),
axis.title.x = element_text(size = 20, face="bold"),
axis.title.y = element_text(size = 20, face="bold"),
axis.text.x = element_text(size =18, face="bold"),
axis.text.y = element_text(size = 18, face="bold"),
plot.margin = unit(c(0.1,0.1,0.1,0.1), "in"))+
guides(shape = guide_legend(override.aes = list(size = 5)))
plot_lumifreq = plot_lumifreq +
geom_segment(aes(x=3, xend=1, y=-Inf, yend=-Inf), size = 2.5, color = 'black') +
geom_segment(aes(y=0, yend=8, x=-Inf, xend=-Inf), size = 2.5, color = 'black')
plot_lumifreq
#ggsave('lumino_freq.png', plot_lumifreq, units = 'in',  width = 9, height = 12)
#####################################################################
lumino_freq$Setting = as.factor(lumino_freq$Setting) # this one!
lumino_freq$Depth = as.factor(lumino_freq$Depth)
#ANCOVA
fit2=aov(bioturbated_area ~ Setting + Depth, data = lumino_freq)
Anova(fit2, type="III")
#Post-hoc
posth=glht(fit2, linfct=mcp(Setting="Tukey"))
summary(posth)
#check data
leveneTest(bioturbated_area ~ Setting * Depth, data = lumino_freq) #plot(fit2)
aov_residuals <- residuals(object = fit2)
shapiro.test(x = aov_residuals)
qqnorm(lumino_freq$bioturbated_area, pch = 1, frame = FALSE)
qqline(lumino_freq$bioturbated_area, col = "steelblue", lwd = 2)
library("car")
qqPlot(lumino_freq$bioturbated_area)
