###Sexual selection on a female copulatory device in an insect with nuptial gifts###
#Behavioural Ecology and Sociobiology
#Jessica H. Browne1,2
#Darryl T. Gwynne1

#1 Department of Ecology and Evolutionary Biology, University of Toronto Mississauga, Mississauga, Ontario L5L 1C6, Canada
#2 E-mail: jbrowne@mta.ca, Current address: Department of Biology Mount Allison University, 62 York St. Sackville, NB, E4L 1E2, Canada

library(readr)
library(ggplot2)
library(MASS) #residuals
library(car)
library(psych)
library(permutes) 
library(buildmer)
library(nlme)
library(lmerTest)

####################################### **FEMALE BATEMAN GRADIENTS** ############################################

attach(bateman)
View(bateman)

str(bateman)
bateman$year <-as.factor(bateman$year)
bateman$site <-as.factor(bateman$site)
bateman$mates <-as.numeric(mates)
bateman$total.offspring <-as.numeric(total.offspring)
bateman$mature.offspring <-as.numeric(mature.offspring)

###Create standardized variables##
mn.size<-mean(size, na.rm=TRUE, data=bateman)
bateman[,8] <-(bateman[,4]/mn.size)

mn.mates<-mean(mates, na.rm=TRUE, data=bateman)
bateman[,9] <-(bateman[,5]/mn.mates)

mn.toffspring<-mean(total.offspring, na.rm=TRUE, data=bateman)
bateman[,10]  <-(bateman[,6]/mn.toffspring)

mn.moffspring<-mean(mature.offspring, na.rm=TRUE, data=bateman)
bateman[,11]  <-(bateman[,7]/mn.moffspring)


########## USING TOTAL EGGS LAID ############
totbateman.lm <-lm(total.offspring~ mates, data=bateman) #raw gradient 
summary(totbateman.lm) 

S.totbateman.lm <- lm(R.totoffspring ~ R.mates, data = bateman) #standardized gradient
summary(S.totbateman.lm) 

totbateman.perms <- perm.glmer(R.totoffspring~ R.mates, data=bateman, nperm=5000, type='regression') #permutation for p values
totbateman.perms

plot(mates, total.offspring) #visualize trend 
abline(lm(total.offspring~mates))

#test assumptions
plot(totbateman.lm)
shapiro.test(resid(totbateman.lm)) #normality of residuals
hist(resid(totbateman.lm))


############ USING DEVELOPED EGGS ############
matbateman.lm <-lm(mature.offspring~ mates, data=bateman) #raw gradient
summary(matbateman.lm)

S.matbateman.lm <- lm(R.matoffspring ~ R.mates + year, data = bateman) #standardized gradient
summary(S.matbateman.lm)

matbateman.perms <- perm.lm(R.matoffspring~ R.mates, data=bateman, nperm=5000, type='regression') #permutation for p values
matbateman.perms

plot(mates, mature.offspring) #visualize trend
abline(lm(mature.offspring~mates))

#assumptions
plot(matbateman.lm)
shapiro.test(resid(matbateman.lm)) #normality of residuals
hist(resid(matbateman.lm))


################# EFFECTS OF BODY SIZE ##################

### BODY SIZE ALONE ###
sizetot.lm <- lm(R.totoffspring~ R.size + R.mates, data=bateman) #eggs laid 
summary(sizetot.lm)
sizetot.perms <- perm.lm(R.totoffspring~ R.size + R.mates, data=bateman[complete.cases(bateman[,c("R.size")]),], nperm=5000, type='regression') #permutation for p values
sizetot.perms

sizemat.lm <- lm(R.matoffspring~ R.size + R.mates, data=bateman) #eggs developed 
summary(sizemat.lm)
sizemat.perms <- perm.lm(R.matoffspring~ R.size +R.mates, data=bateman[complete.cases(bateman[,c("R.size")]),], nperm=5000, type='regression') #permutation for p values
sizemat.perms

### SIZE INCLUDED IN BATEMAN MODEL ###
size.totbateman.lm <- lm(R.totoffspring~ R.size + R.mates, data=bateman) #total eggs laid 
summary(size.totbateman.lm)
size.totbateman.perms <- perm.lm(R.totoffspring~ R.size + R.mates, data=bateman[complete.cases(bateman[,c("R.size")]),], nperm=5000, type='regression') #permutation for p values
size.totbateman.perms

size.matbateman.lm <- lm(R.matoffspring~ R.size + R.mates, data=bateman) #eggs developed
summary(size.matbateman.lm)
size.matbateman.perms <- perm.lm(R.matoffspring~ R.size + R.mates, data=bateman[complete.cases(bateman[,c("R.size")]),], nperm=5000, type='regression') #permutation for p values
size.matbateman.perms

##################### INTERACTIONS WITH SITE AND YEAR #####################
#Visualize

ggplot() +
  geom_smooth(data = d, aes(x=mates,y=total.offspring), method=lm, size=1.5, se=TRUE, level=0.95, color=1, fill="grey80", fullrange=T) +
  geom_point(data = d, aes(x = mates, y = total.offspring), shape= "circle", color=1, size=1.5, position= position_jitter(width=0.4, height=0)) + #POINT DETAILS
  geom_smooth(data = d, aes(x=mates, y=total.offspring, linetype= site, color= site), method=lm, size=0.75, se=F, level=F, fullrange=T) +
  geom_smooth(data = d, aes(x=mates,y=total.offspring, linetype=year, color = year), method=lm, size=0.75, se=F, level=F, fullrange=T)

ggplot() +
  geom_smooth(data = d, aes(x=mates,y=mature.offspring), method=lm, size=1.5, se=TRUE, level=0.95, color=1, fill="grey80", fullrange=T) +
  geom_point(data = d, aes(x = mates, y = mature.offspring), shape= "circle", color=1, size=1.5, position= position_jitter(width=0.4, height=0)) + #POINT DETAILS
  geom_smooth(data = d, aes(x=mates, y=mature.offspring, linetype= site, color= site), method=lm, size=0.75, se=F, level=F, fullrange=T) +
  geom_smooth(data = d, aes(x=mates,y=mature.offspring, linetype=year, color = year), method=lm, size=0.75, se=F, level=F, fullrange=T)

##TEST SITE INTERACTIONS
site.totbateman.lm <-lm(R.totoffspring~ R.mates +site*R.mates, data=bateman) #total eggs laid
summary(site.totbateman.lm)

site.matbateman.lm <-lm(R.matoffspring~ R.mates +site*R.mates, data=bateman) #developed eggs
summary(site.matbateman.lm)


##TEST YEAR INTERACTIONS
year.totbateman.lm <-lm(R.totoffspring~ R.mates + year*R.mates, data=bateman) #eggs laid 
summary(year.totbateman.lm)

year.matbateman.lm <-lm(R.matoffspring~ R.mates +year*R.mates, data=bateman) #developed eggs
summary(year.matbateman.lm)


#### BATEMAN GRADIENTS SEPARATED BY YEAR ####

##2002 - restandardize variables
attach(bateman02)
mn.size<-mean(size, na.rm=TRUE, data=bateman02)
bateman02[,8]<-(bateman02[,4]/mn.size)

mn.mates<-mean(mates, na.rm=TRUE, data=bateman02)
bateman02[,9]<-(bateman02[,5]/mn.mates)

mn.toffspring<-mean(total.offspring, na.rm=TRUE, data=bateman02)
bateman02[,10] <-(bateman02[,6]/mn.toffspring)

mn.moffspring<-mean(mature.offspring, na.rm=TRUE, data=bateman02)
bateman02[,11] <-(bateman02[,7]/mn.moffspring)

##Total Offspring 2002
tot.lm02 <- lm(total.offspring ~  mates, data=bateman02) #Raw 2002 
summary(tot.lm02)
tot.lm02 <- lm(R.totoffspring ~  R.mates, data=bateman02) #Standardized 2002 
summary(tot.lm02)
tot.perms02<- perm.lm(R.totoffspring~ R.mates, data=bateman02, nperm=5000, type='regression') #permutation for p values
tot.perms02

#Developed offspring 2002 *ESM ONLY
mat.lm02 <- lm(mature.offspring ~ mates, data=bateman02) #Raw 2002
summary(mat.lm02)
mat.lm02 <- lm(R.matoffspring ~ R.mates, data=bateman02) #Standardized 2002
summary(mat.lm02)
mat.perms02 <- perm.lm(R.matoffspring~ R.mates, data=bateman02, nperm=5000, type='regression') #permutation for p values
mat.perms02

#test assumptions
plot(tot.lm02)
shapiro.test(resid(tot.lm02)) #normality of residuals
hist(resid(tot.lm02))

plot(mat.lm02)
shapiro.test(resid(mat.lm02)) #normality of residuals
hist(resid(mat.lm02))


###2017 - restandardize variables
attach(bateman17)
mn.size<-mean(size, na.rm=TRUE, data=bateman17)
bateman17[,8]<-(bateman17[,4]/mn.size)

mn.mates<-mean(mates, na.rm=TRUE, data=bateman17)
bateman17[,9]<-(bateman17[,5]/mn.mates)

mn.toffspring<-mean(total.offspring, na.rm=TRUE, data=bateman17)
bateman17[,10] <-(bateman17[,6]/mn.toffspring)

mn.moffspring<-mean(mature.offspring, na.rm=TRUE, data=bateman17)
bateman17[,11] <-(bateman17[,7]/mn.moffspring)

#Total offspring 2017
tot.lm17 <- lm(total.offspring ~  mates, data=bateman17) #Raw 2017 
summary(tot.lm17)
tot.lm17 <- lm(R.totoffspring ~  R.mates, data=bateman17) #Standardized 2017 
summary(tot.lm17)
tot.perms17<- perm.lm(R.totoffspring~ R.mates, data=bateman17, nperm=5000, type='regression') #permutation for p values
tot.perms17

#Developed offspring 2017 *ESM ONLY
mat.lm17 <- lm(mature.offspring ~ mates, data=bateman17) #Raw 2017 
summary(mat.lm17)
mat.lm17 <- lm(R.matoffspring ~ R.mates, data=bateman17) #Standardized 2017 
summary(mat.lm17)
mat.perms17 <- perm.lm(R.matoffspring~ R.mates, data=bateman17, nperm=5000, type='regression') #permutation for p values
mat.perms17

#test assumptions
plot(tot.lm17)
shapiro.test(resid(tot.lm17)) #normality of residuals
hist(resid(tot.lm17))

plot(mat.lm17)
shapiro.test(resid(mat.lm17)) #normality of residuals
hist(resid(mat.lm17))


#### BATEMAN GRADIENTS SEPARATED BY SITE *ESM ONLY ####

#Kiri - restandardize variables 
attach(batemanK)
mn.size<-mean(size, na.rm=TRUE, data=batemanK)
batemanK[,8]<-(batemanK[,4]/mn.size)

mn.mates<-mean(mates, na.rm=TRUE, data=batemanK)
batemanK[,9]<-(batemanK[,5]/mn.mates)

mn.toffspring<-mean(total.offspring, na.rm=TRUE, data=batemanK)
batemanK[,10] <-(batemanK[,6]/mn.toffspring)

mn.moffspring<-mean(mature.offspring, na.rm=TRUE, data=batemanK)
batemanK[,11] <-(batemanK[,7]/mn.moffspring)

Kiri.tot.lm <- lm(total.offspring ~  mates, data=batemanK) #Raw Kiriwhakapapa total eggs laid
summary(Kiri.tot.lm)
Kiri.tot.lm <- lm(R.totoffspring ~  R.mates, data=batemanK) #Standardized Kiriwhakapapa total eggs laid
summary(Kiri.tot.lm)
Kiri.tot.perms<- perm.lm(R.totoffspring~ R.mates, data=batemanK, nperm=5000, type='regression') #permutation for p values
Kiri.tot.perms

Kiri.mat.lm <- lm(mature.offspring ~ mates, data=batemanK) #Raw Kiriwhakapapa developed eggs 
summary(Kiri.mat.lm)
Kiri.mat.lm <- lm(R.matoffspring ~ R.mates, data=batemanK) #Standardized Kiriwhakapapa developed eggs 
summary(Kiri.mat.lm)
Kiri.mat.perms<- perm.lm(R.matoffspring~ R.mates, data=batemanK, nperm=5000, type='regression') #permutation for p values
Kiri.mat.perms

#test assumptions
plot(Kiri.tot.lm)
shapiro.test(resid(Kiri.tot.lm)) #normality of residuals
hist(resid(Kiri.tot.lm))

plot(Kiri.mat.lm)
shapiro.test(resid(Kiri.mat.lm)) #normality of residuals
hist(resid(Kiri.mat.lm))


#Pamly - restandardize variables #
attach(batemanPN)
mn.size<-mean(size, na.rm=TRUE, data=batemanPN) 
batemanPN[,8]<-(batemanPN[,4]/mn.size)

mn.mates<-mean(mates, na.rm=TRUE, data=batemanPN)
batemanPN[,9]<-(batemanPN[,5]/mn.mates)

mn.toffspring<-mean(total.offspring, na.rm=TRUE, data=batemanPN)
batemanPN[,10] <-(batemanPN[,6]/mn.toffspring)

mn.moffspring<-mean(mature.offspring, na.rm=TRUE, data=batemanPN)
batemanPN[,11] <-(batemanPN[,7]/mn.moffspring)

PN.tot.lm <- lm(total.offspring ~  mates, data=batemanPN) #Raw Palmy eggs laid
summary(PN.tot.lm)
PN.tot.lm <- lm(R.totoffspring ~  R.mates, data=batemanPN) #Standardized Palmy eggs laid
summary(PN.tot.lm)
PN.tot.perms<- perm.lm(R.totoffspring~ R.mates, data=batemanPN, nperm=5000, type='regression') #permutation for p values
PN.tot.perms

PN.mat.lm <- lm(mature.offspring ~ mates, data=batemanPN) #Raw Palmy developed eggs
summary(PN.mat.lm)
PN.mat.lm <- lm(R.matoffspring ~ R.mates, data=batemanPN) #Standardized Palmy developed eggs
summary(PN.mat.lm)
PN.mat.perms<- perm.lm(R.matoffspring~ R.mates, data=batemanPN, nperm=5000, type='regression') #permutation for p values
PN.mat.perms

#test assumptions
plot(PN.tot.lm)
shapiro.test(resid(PN.tot.lm)) #normality of residuals
hist(resid(PN.tot.lm))

plot(PN.mat.lm)
shapiro.test(resid(PN.mat.lm)) #normality of residuals
hist(resid(PN.mat.lm))


########################BATEMAN FIGURE INCLUDED IN PUBLICATION############################
bateman$year <-as.factor(bateman$year)
d<-bateman
my_font='Arial'

#Total eggs 
P1 <-
  ggplot() +
  geom_smooth(data = d, aes(x=mates,y=total.offspring), method=lm, size=1.5, se=TRUE, level=0.95, color=1, fill="grey80", fullrange=T) +
  geom_point(data = d, aes(x = mates, y = total.offspring), shape= "circle", color=1, size=1.5, position= position_jitter(width=0.4, height=0)) + #POINT DETAILS
  geom_smooth(data = d, aes(x=mates, y=total.offspring, linetype= site, color= site), method=lm, size=0.75, se=F, level=F, fullrange=T) +
  geom_smooth(data = d, aes(x=mates,y=total.offspring, linetype=year, color = year), method=lm, size=0.75, se=F, level=F, fullrange=T) +
  geom_text(data=d, x=0.1, y=80, label= "A", size=6, family=my_font) +
  
  scale_color_manual(values=c("grey70", "grey40", "grey70", "grey40")) +
  scale_linetype_manual(values = c("longdash", "longdash", "twodash", "twodash")) +
  
  labs(y="Number of eggs laid", x="") + #AXIS LABELS
  scale_x_continuous(limits=c(0,8), breaks=seq(0,8, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(0,80), breaks=seq(0,80, by=10)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3),
        legend.position = "none"
  )
plot(P1)


##Developed Eggs
P2<-
  ggplot() +
  geom_smooth(data = d, aes(x=mates,y=mature.offspring), method=lm, size=1.5, se=TRUE, level=0.95, color=1, fill="grey80", fullrange=T) +
  geom_point(data = d, aes(x = mates, y = mature.offspring), shape= "circle", colour =1, size=1.5, position= position_jitter(width=0.4, height=0)) + #POINT DETAILS
  geom_smooth(data = d, aes(x=mates,y=mature.offspring, linetype=site, color=site), method=lm, size=0.75, se=F, level=F, fullrange=T) +
  geom_smooth(data = d, aes(x=mates,y=mature.offspring, linetype=year, color=year), method=lm, size=0.75, se=F, level=F, fullrange=T) +
  geom_text(data=d, x=0.1, y=60, label= "B", size=6, family=my_font) +
  
  scale_color_manual(values=c("grey70", "grey40", "grey70", "grey40"), name = "Collections", labels = c("2002", "2017", "Kiriwhakapapa", "Palmerston North")) +
  scale_linetype_manual(values = c("longdash", "longdash", "twodash", "twodash"), name = "Collections", labels = c("2002", "2017", "Kiriwhakapapa", "Palmerston North")) +
  
  labs(y="Number of developed eggs", x="") + #AXIS LABELS
  scale_x_continuous(limits=c(0,8), breaks=seq(0,8, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(0,60), breaks=seq(0,60, by=10)) +
  
  theme_bw()+ #background
  theme(legend.text = element_text(size=12, family=my_font), legend.title = element_text(size=14, family=my_font),
        axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3),
  )

plot(P2)

## COMBINE FIGURES ##
install.packages("devtools")
devtools::install_github("thomasp85/patchwork")

library(patchwork)
P1+P2



##############################**SEXUAL SELECTION ON ACCESSORY ORGANS**##############################
library(readr)
library(MASS) #residuals
library(car)
library(ggplot2)
library(fields)
library(psych)

attach(females.ss)

str(females.ss)
females.ss$year = as.factor(females.ss$year) 
females.ss$site <-as.factor(site)
females.ss$mates <-as.numeric(mates)

###STANDARDIZE VARIABLES##
females.ss$body.size <- scale(females.ss$body.size)
females.ss$AO.length <- scale(females.ss$AO.length)
females.ss$AO.width <- scale(females.ss$AO.width)

mn.mates<-mean(mates, na.rm=TRUE, data=females.ss)
females.ss[,7]<-(females.ss[,7] / mn.mates)

mneggs<-mean(eggs, na.rm=TRUE, data=female.ss)
females.ss[,8]<-(females.ss[,8]/mneggs)

####RUN MODEL####
females.lm <- lm(mates ~ AO.length + AO.width + body.size, data= females.ss)
summary(females.lm)
females.perms <- perm.lm(mates ~ AO.length + AO.width + body.size, data= females.ss, nperm=5000, type='regression') #permutation for p values
females.perms

#include year as random effect *SINGULAR FIT; NOT ENOUGH POWER 
mates.lm <- lmer(mates ~ AO.length + AO.width + body.size +(1|year), data=females.ss) 
summary(mates.lm)
mates.perms.R <- perm.lmer(mates ~ AO.length + AO.width + body.size +(1|year), data= females.ss, nperm=5000, type='regression') #permutation for p values
mates.perms.R

###Check Assumptions
plot(females.lm)
shapiro.test(resid(females.lm)) #normality of residuals
hist(resid(females.lm))


######VISUALIZE VARITATION BETWEEN SITES AND YEARS######
ggplot(data=females.ss, aes(x= body.size, y = mates, color=site)) + geom_point() + geom_smooth(method = lm) #test further
ggplot(data=females.ss, aes(x= AO.length, y = mates, color=site)) + geom_point() + geom_smooth(method = lm) #no issue
ggplot(data=females.ss, aes(x= AO.width, y = mates, color=site)) + geom_point() + geom_smooth(method = lm) #test further

ggplot(data=females.ss, aes(x= body.size, y = mates, color=year)) + geom_point() + geom_smooth(method = lm)
ggplot(data=females.ss, aes(x= AO.length, y = mates, color=year)) + geom_point() + geom_smooth(method = lm)
ggplot(data=females.ss, aes(x= AO.width, y = mates, color=year)) + geom_point() + geom_smooth(method = lm)


####TEST INTERACTIONS WITH SITE####
interaction.lm1 <- lm(mates ~  body.size  + body.size*site, data= females.ss)
summary(interaction.lm1)

interaction.lm3 <- lm(mates ~  AO.width+ AO.width*site, data= females.ss)
summary(interaction.lm3)

interaction.lm2 <- lm(mates ~  AO.length+ AO.length*site, data= females.ss)
summary(interaction.lm2)


######## VARIATION ACROSS SITES *PLOTS INCLUDED IN ESM#######

#SITE
#body size
PA <-
  ggplot() +
  geom_point(data=females.ss, aes(x= body.size, y = mates, color=site, shape=site)) + #POINT DETAILS
  geom_smooth(data=females.ss, aes(x= body.size, y = mates, color=site, linetype=site), method=lm, se=T, fill="grey70") +
  geom_smooth(data=females.ss, aes(x= body.size, y = mates), method=lm, color=1, se=F) +
  
  scale_color_manual(values=c("lightcoral", "royalblue3"), name = "Collections", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_linetype_manual(values = c("dotdash", "dotdash"), name = "Collections", labels = c("Kiriwhakapapa", "Palmerston North")) +
  
  labs(y="Relative number of mates", x="Body size") + #AXIS LABELS
  scale_x_continuous(limits=c(-2.5,3), breaks=seq(-2.5,3, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(-0.75,3), breaks=seq(-0.75,3, by=0.5)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3),
        legend.position = "none"
  )
plot(PA)

#Organ width
PB <-
  ggplot() +
  geom_point(data=females.ss, aes(x= AO.width, y = mates, color=site, shape=site)) + #POINT DETAILS
  geom_smooth(data=females.ss, aes(x= AO.width, y = mates, color=site, linetype=site), method=lm, se=T, fill="grey70") +
  geom_smooth(data=females.ss, aes(x= AO.width, y = mates), method=lm, color=1, se=F) +
  
  scale_color_manual(values=c("lightcoral", "royalblue3"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_linetype_manual(values = c("dotdash", "dotdash"), name = "Collections Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  
  labs(y="", x="Accesory organ width") + #AXIS LABELS
  scale_x_continuous(limits=c(-2,2.5), breaks=seq(-2,2.5, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(-0.5,3), breaks=seq(-0.5,3, by=0.5)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3),
        legend.position = "none" 
  )
plot(PB)

#organ length
PC <-
  ggplot() +
  geom_point(data=females.ss, aes(x= AO.length, y = mates, color=site, shape=site)) + #POINT DETAILS
  geom_smooth(data=females.ss, aes(x= AO.length, y = mates, color=site, linetype=site), method=lm, se=TRUE, fill="grey70") +
  geom_smooth(data=females.ss, aes(x= AO.length, y = mates), method=lm, color=1, se=F) +
  
  guides(color=guide_legend(override.aes=list(fill=NA)))+ 
  
  scale_shape_manual(values=c("circle", "triangle"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_color_manual(values=c("lightcoral", "royalblue3"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_linetype_manual(values = c("dotdash", "dotdash"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  
  labs(y="", x="Accesory organ length") + #AXIS LABELS
  scale_x_continuous(limits=c(-2,3), breaks=seq(-2,2.5, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(-0.5,3), breaks=seq(-0.5,3, by=0.5)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3), 
        legend.key.size = unit(1, 'cm'), legend.text = element_text(size=10)
        
  )
plot(PC)

PA + PB + PC


#########################**RELATIONSHIP WITH NUMBER OF EGSS** ##################################

Eggs.lm <- lm(eggs ~ AO.length + AO.width + body.size, data=females.ss) #does AO size correlate with fecundity? 
summary(Eggs.lm)
Eggs.perms <- perm.lm(eggs ~ AO.length + AO.width + body.size, data= females.ss, nperm=5000, type='regression') #permutation for p values
Eggs.perms

#include year as random effects 
Eggs.lm <- lmer(eggs ~ AO.length + AO.width + body.size +(1|year), data=females.ss) #does AO size correlate with fecundity? 
summary(Eggs.lm)
Eggs.perms.R <- perm.lmer(eggs ~ AO.length + AO.width + body.size +(1|year), data= females.ss, nperm=5000, type='regression') #permutation for p values
Eggs.perms.R

plot(Eggs.lm)


#####VISUALIZE VARIATION ACROSS SITES AND YEARS ####
ggplot(data=females.ss, aes(x= body.size, y = eggs, color=site)) + geom_point() + geom_smooth(method = lm) #test further
ggplot(data=females.ss, aes(x= AO.length, y = eggs, color=site)) + geom_point() + geom_smooth(method = lm) #no issue
ggplot(data=females.ss, aes(x= AO.width, y = eggs, color=site)) + geom_point() + geom_smooth(method = lm) #test further

ggplot(data=females.ss, aes(x= body.size, y = eggs, color=year)) + geom_point() + geom_smooth(method = lm)
ggplot(data=females.ss, aes(x= AO.length, y = eggs, color=year)) + geom_point() + geom_smooth(method = lm)
ggplot(data=females.ss, aes(x= AO.width, y = eggs, color=year)) + geom_point() + geom_smooth(method = lm)


#####TEST INTERACTIONS WITH SITE######

interaction.lm1 <- lm(eggs ~  body.size+ body.size*site, data= females.ss)
summary(interaction.lm1)

interaction.lm3 <- lm(eggs ~  AO.width+ AO.width*site, data= females.ss)
summary(interaction.lm3)

interaction.lm2 <- lm(eggs ~  AO.length+ AO.length*site, data= females.ss)
summary(interaction.lm2)



########VARIATION ACROSS SITES *PLOTS INCLUDED IN ESM#######

PD <-
  ggplot() +
  geom_point(data=females.ss, aes(x= body.size, y = eggs, color=site, shape=site)) + #POINT DETAILS
  geom_smooth(data=females.ss, aes(x= body.size, y = eggs, color=site, linetype=site), method=lm, se=T, fill="grey80") +
  geom_smooth(data=females.ss, aes(x= body.size, y = eggs), method=lm, color=1, se=F) +
  
  scale_color_manual(values=c("lightcoral", "royalblue3"), name = "Collections", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_linetype_manual(values = c("dotdash", "dotdash"), name = "Collections", labels = c("Kiriwhakapapa", "Palmerston North")) +
  
  labs(y="Relative number of eggs in abdomen", x="Body size") + #AXIS LABELS
  scale_x_continuous(limits=c(-2.5,3), breaks=seq(-2.5,3, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(-0.5,3), breaks=seq(-0.5,3, by=0.5)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3),
        legend.position = "none"
  )
plot(PD)


PE <-
  ggplot() +
  geom_point(data=females.ss, aes(x= AO.width, y = eggs, color=site, shape=site)) + #POINT DETAILS
  geom_smooth(data=females.ss, aes(x= AO.width, y = eggs, color=site, linetype=site), method=lm, se=T, fill="grey80") +
  geom_smooth(data=females.ss, aes(x= AO.width, y = eggs), method=lm, color=1, se=F) +
  
  scale_color_manual(values=c("lightcoral", "royalblue3"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_linetype_manual(values = c("dotdash", "dotdash"), name = "Collections Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  
  labs(y="", x="Accesory organ width") + #AXIS LABELS
  scale_x_continuous(limits=c(-2,2.5), breaks=seq(-2,2.5, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(-0.5,3), breaks=seq(-0.5,3, by=0.5)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3),
        legend.position = "none" 
  )
plot(PE)


PF <-
  ggplot() +
  geom_point(data=females.ss, aes(x= AO.length, y = eggs, color=site, shape=site)) + #POINT DETAILS
  geom_smooth(data=females.ss, aes(x= AO.length, y = eggs, color=site, linetype=site), method=lm, se=TRUE, fill="grey80") +
  geom_smooth(data=females.ss, aes(x= AO.length, y = eggs), method=lm, color=1, se=F) +
  
  guides(color=guide_legend(override.aes=list(fill=NA)))+ 
  
  scale_shape_manual(values=c("circle", "triangle"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_color_manual(values=c("lightcoral", "royalblue3"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  scale_linetype_manual(values = c("dotdash", "dotdash"), name = "Collection Site", labels = c("Kiriwhakapapa", "Palmerston North")) +
  
  labs(y="", x="Accesory organ length") + #AXIS LABELS
  scale_x_continuous(limits=c(-2,3.5), breaks=seq(-2,3.5, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(-0.5,3), breaks=seq(-0.5,3, by=0.5)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3), 
        legend.key.size = unit(1, 'cm'), legend.text = element_text(size=10)
        
  )
plot(PF)

PD + PE + PF


########VARIATION ACROSS YEARS *PLOT INCLUDED IN ESM#######

Plot.year <-
  ggplot() +
  geom_point(data=females.ss, aes(x= AO.width, y = eggs, color=year, shape= year)) + #POINT DETAILS
  geom_smooth(data=females.ss, aes(x= AO.width, y = eggs, color=year, linetype=year), method=lm, se=TRUE, fill="grey80") +
  geom_smooth(data=females.ss, aes(x= AO.width, y = eggs), method=lm, color=1, se=F) +
  
  guides(color=guide_legend(override.aes=list(fill=NA)))+ 
  
  scale_shape_manual(values=c("circle", "triangle", "square", "diamond"), name = "Collection Year", labels = c("2012", "2016", "2017", "2019")) +
  scale_color_manual(values=c("royalblue3", "lightcoral","darkorchid4", "cyan2"), name = "Collection Year", labels = c("2012", "2016", "2017", "2019")) +
  scale_linetype_manual(values = c("dashed", "dashed", "longdash", "longdash"), name = "Collection Year", labels = c("2012", "2016", "2017", "2019")) +
  
  labs(y="Relative number of eggs in the abdomen", x="Accesory organ width") + #AXIS LABELS
  scale_x_continuous(limits=c(-2,3), breaks=seq(-2,2.5, by=1)) + # LIMITS & INTERVALS
  scale_y_continuous(limits=c(-0.5,3), breaks=seq(-0.5,3, by=0.5)) +
  
  theme_bw()+ #background
  theme(axis.line = element_line(),panel.grid.minor=element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_blank(),
        axis.text.x=element_text(size=12, color= "black", family=my_font), axis.title.x=element_text(size=12, family=my_font,vjust=0.01),
        axis.text.y=element_text(size=12, color= "black", family=my_font), axis.title.y=element_text(size=12, family=my_font,vjust=3), 
        legend.key.size = unit(1, 'cm'), legend.text = element_text(size=10)
        
  )
plot(Plot.year)

