#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Example script to generate the output used in Yesbol Manat et al., Biology Letter #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

library(lme4)    # GLMMs
library(blmeco)  # Overdispersion test
library(car)     # Anova
library(emmeans) # Post-hoc test.
library(glmmTMB) # glmmTMB
library(DHARMa)  # Overdispersion test

######### male mating attractivness
mating_attractiveness <- read_excel("Desktop/mating.attractiveness.xlsx",col_types = c("numeric", "text", "numeric","numeric", "numeric"))
mating<-as.data.frame(mating_attractiveness)
for(x in 1:4)mating[,x]<-as.factor(mating[,x])

# male mating success (MS by mating frequency).
MS <- glmer(mating~treatment+(1|treatment:replicate)+block, family="binomial", data=mating)
summary(MS)
AIC(MS) #  150.9448
dispersion_glmer(MS) # 0.7608789
Anova(MS, Type="III")
drop1(MS, test="Chisq")
emmeans(MS,list(pairwise ~treatment),adjust="tukey")

# male attractiveness-mating latency(ML).
ML<-glmer(time~treatment+(1|treatment:replicate), family ="Gamma", data=mating)
AIC(ML)
dispersion_glmer(ML)  # 0.8270685
Anova(ML, Type="III")
drop1(ML, test="Chisq")
emmeans(ML,list(pairwise ~treatment),adjust="tukey")


####### Defense-P1, 
# Where the experimental male (red eyed) is the first male to mate and competitor male (brown eyed male:LHM-bw) is the second male to mate. 
Defense <- read_excel("Desktop/Defense.xlsx",col_types = c("text", "text", "numeric","numeric"))
Defense<-as.data.frame(Defense)
for(x in 1:2)Defense[,x]<-as.factor(Defense[,x])
Defense$all<-Defense$red.eyed+Defense$brown.eyed # created a variable to remove the vials which has less than 10 offspring from both males:all=brown+red
Defense$all<-ifelse(Defense$all>=10,Defense$all,"NA") ## removed vials which has less than 10 offspring (brown and red eyed)
Defense$all<-as.numeric(Defense$all)
Defense$p1<-Defense$red.eyed/Defense$all

p1<-glmmTMB(cbind(red.eyed,brown.eyed)~treatment+(1|treatment:replicate),ziformula = ~1, data=Defense, family = binomial)
summary(p1)
Anova(p1, type=3)
emmeans(p1,list(pairwise ~treatment),adjust="tukey")
simulationOutput <- simulateResiduals(fittedModel = p1, plot = T)
simulationOutput <-recalculateResiduals(simulationOutput, group = Defense$treatment) # Calculating residuals per group
testDispersion(simulationOutput)


####### Offense-P2,
# Where the experimental male (red eyed) is the second male to mate and competitor male (brown eyed male:LHM-bw) is the first male to mate. 

Offense <- read_excel("Desktop/Offense.xlsx",col_types = c("text", "text", "numeric","numeric", "numeric"))
Offense<-as.data.frame(Offense)
for(x in 1:3)Offense[,x]<-as.factor(Offense[,x])
Offense$all<-Offense$red.eyed+Offense$brown.eyed ## created a variable to remove the vials which has less than 10 offspring from both males:all=brown+red
Offense$all<-ifelse(Offense$all>=10,Offense$all,"NA") ## removed vials which has less than 10 offspring (brown and red eyed)
Offense$all<-as.numeric(Offense$all,na.rm=TRUE)
Offense$p2<-Offense$red.eyed/Offense$all

p2<-glmmTMB(cbind(brown.eyed,red.eyed)~treatment+block+(1|treatment:replicate),ziformula = ~1, data=Offense, family = binomial("logit"))
summary(p2)
Anova(p2, type=3)
emmeans(p2,list(pairwise ~treatment),adjust="tukey")
simulationOutput <- simulateResiduals(fittedModel = p2, plot = T)
simulationOutput <-recalculateResiduals(simulationOutput, group = Offense$treatment) 
testDispersion(simulationOutput)

## visualizations ##
library(ggplot2)
library(ggpubr)
library(Hmisc)
# Figure 1B,C,D: Graphical representation
# Figure 1B, FLX effects
n3<-c(1,2,2)
tre<-factor(c("FLX","CFM","CWT"))
d3<-data.frame(Treatment=tre,Trait=n3)
str(d3)
d3
d3$Treatments<-factor(d3$Treatment,as.character(d3$Treatment)) # re-order the levels in the order of appearance in the data.frame
FLX.effect<-ggplot(d3, aes(x=Treatments, y=Trait)) + geom_bar(stat = "identity", fill=c("#FF3333", "#00CCCC","#009966"),
                                                              color =c("#FF3333", "#00CCCC","#009966"),width = 0.6) +
  ggtitle("FLX effects")+
  theme(axis.line = element_line(colour = "black",size = 0.5),
        plot.title = element_text(color="black", size=22,hjust = 0.5),
        axis.text.x=element_text(face="plain",color="black",size=20),
        axis.title.x = element_text(color="black", size=20),
        axis.title.y = element_text(color="black", size=20),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        panel.background = element_blank())+
  scale_y_discrete(expand = c(0,0))
print(FLX.effect+labs(y="Male performance", x="Regimes"))
# Figure 1C, FM effects
n1<-c(1,1,2)
tre<-factor(c("FLX","CFM","CWT"))
d1<-data.frame(Treatment=tre,Trait=n1)
d1$Treatments<-factor(d1$Treatment,as.character(d1$Treatment)) 
FM.effects<-ggplot(d1, aes(x=Treatments, y=Trait)) + geom_bar(stat = "identity", fill=c("#FF3333", "#00CCCC","#009966"),
                                                             color =c("#FF3333", "#00CCCC","#009966"),width = 0.6) +
  ggtitle("FM effects")+
  theme(axis.line = element_line(colour = "black",size = 0.5),
        plot.title = element_text(color="black", size=22,hjust = 0.5),
        axis.text.x=element_text(face="plain",color="black",size=20),
        axis.title.x = element_text(color="black", size=20),
        axis.title.y = element_text(color="black", size=20),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        panel.background = element_blank())+
  scale_y_discrete(expand = c(0,0))
print(FM.effects+labs(y="Male performance", x="Regimes"))
# Figure 1D, CFMvsFLX effects
n2<-c(1,2,1)
tre<-factor(c("FLX","CFM","CWT"))
d2<-data.frame(Treatment=tre,Trait=n2)
str(d2)
d2 
d2$Treatments<-factor(d2$Treatment,as.character(d2$Treatment)) 
CFMvsFLX.effects<-ggplot(d2, aes(x=Treatments, y=Trait)) + geom_bar(stat = "identity", fill=c("#FF3333", "#00CCCC","#009966"),
                                                                    color =c("#FF3333", "#00CCCC","#009966"),width = 0.6) +
  ggtitle("FLXvsFM effects")+
  theme(axis.line = element_line(colour = "black",size = 0.5),
        plot.title = element_text(color="black", size=22,hjust = 0.5),
        axis.text.x=element_text(face="plain",color="black",size=20),
        axis.title.x = element_text(color="black", size=20),
        axis.title.y = element_text(color="black", size=20),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        panel.background = element_blank())+
  scale_y_discrete(expand = c(0,0))
print(CFMvsFLX.effects+labs(y="Male performance", x="Regimes"))

# Figure 2A, Male mating success
ggplot(ma2,aes(x=Treatments,y=Counts,fill=factor(mating)))+
  ggtitle("Male mating success")+
  geom_bar(stat="identity",position="dodge")+
  scale_fill_discrete(name="Mating",
                      breaks=c(1, 0),
                      labels=c("mated", "unmated"))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))+
  theme(plot.title = element_text(color="black", size=14, hjust = 0.5,vjust=0),
        legend.text = element_text(colour="black", size = 13),
        axis.text.x = element_text(colour="black", size = 13))

# Figure 2B, Copulation latency
par(mar = c(2, 4, 2, 2))
ma<-as.data.frame(mating)
flx<-subset(ma,ma$treatment=="FLX")
cfm<-subset(ma,ma$treatment=="CFM")
cwt<-subset(ma,ma$treatment=="CWT")
mean.flx<-mean(flx$time) # 10.12604 min
mean.cfm<-mean(cfm$time) 
mean.cwt<-mean(cwt$time) 
rep1flx<-subset(flx,flx$replicate==1)
rep2flx<-subset(flx,flx$replicate==2)
rep3flx<-subset(flx,flx$replicate==3)
rep4flx<-subset(flx,flx$replicate==4)
rep1flx.m<-mean(rep1flx$time) 
rep2flx.m<-mean(rep2flx$time) 
rep3flx.m<-mean(rep3flx$time)
rep4flx.m<-mean(rep4flx$time) 
rep.flx.m<-c(rep1flx.m,rep2flx.m,rep3flx.m,rep4flx.m)
rep1cfm<-subset(cfm,cfm$replicate==1)
rep2cfm<-subset(cfm,cfm$replicate==2)
rep3cfm<-subset(cfm,cfm$replicate==3)
rep4cfm<-subset(cfm,cfm$replicate==4)
rep1cfm.m<-mean(rep1cfm$time)
rep2cfm.m<-mean(rep2cfm$time)
rep3cfm.m<-mean(rep3cfm$time)
rep4cfm.m<-mean(rep4cfm$time)
rep.cfm.m<-c(rep1cfm.m,rep2cfm.m,rep3cfm.m,rep4cfm.m)
rep1cwt<-subset(cwt,cwt$replicate==1)
rep2cwt<-subset(cwt,cwt$replicate==2)
rep3cwt<-subset(cwt,cwt$replicate==3)
rep4cwt<-subset(cwt,cwt$replicate==4)
rep1cwt.m<-mean(rep1cwt$time)
rep2cwt.m<-mean(rep2cwt$time)
rep3cwt.m<-mean(rep3cwt$time)
rep4cwt.m<-mean(rep4cwt$time)
rep.cwt.m<-c(rep1cwt.m,rep2cwt.m,rep3cwt.m,rep4cwt.m)
se.flx<-(sd(flx$time))/(sqrt(length(na.omit(flx$time)))) 
se.cfm<-(sd(cfm$time))/(sqrt(length(na.omit(cfm$time)))) 
se.cwt<-(sd(cwt$time))/(sqrt(length(na.omit(cwt$time)))) 
Mean<-c(mean.flx,mean.cfm,mean.cwt)
Se<-c(se.flx,se.cfm,se.cwt)
x<-c(1:3)
Mean
plot(x,Mean,type="b",lty=0, xlim=c(0.5,3.5), xlab = "",
     col=c("#FF3333", "#00CCCC","#009966"),
     ylim=c(7, 17), ylab="",cex = 1.8,las=2,
     xaxt='n',main="",pch = c(15, 16, 17))
title(main ="Copulation latency",line=0.5,cex.main=1.6,font.main=1)
title(ylab ="Copulation latency in minutes",line = 2,cex.lab=1.6)
axis(1, at=c(1,2,3),labels = c("FLX","CFM","CWT"),cex.axis=1.4)
lines(x, Mean, col="gray",lty=1)
# legend(0.33,17.5,inset=0,legend = c("FLX","CFM","CWT"), pch = c(15, 16, 17),cex = 1.6, bty = "n",x.intersp=0.4,col=c("#FF3333", "#00CCCC","#009966"))
errbar(x, Mean,Mean+Se,Mean-Se,add=TRUE,errbar.col=c("#FF3333", "#00CCCC","#009966"),pch = 27,lwd=1.8)
points(jitter(rep(1, 4), -0.15, 0.15), rep.flx.m,pch = 15, cex = 1, col = "#FF6633")
points(jitter(rep(2, 4), -0.15, 0.15), rep.cfm.m,pch = 16, cex = 1, col = "#009999")
points(jitter(rep(3, 4), -0.15, 0.15), rep.cwt.m,pch = 17, cex = 1, col = "#009900")


# Figure 2C, Defence ability 
Defense$p1<-Defense$red.eyed/Defense$all
str(Defense)
de<-na.omit(Defense)
flx<-subset(de,de$treatment=="FLX")
cfm<-subset(de,de$treatment=="CFM")
cwt<-subset(de,de$treatment=="CWT")
rep1flx<-subset(flx,flx$replicate=="Rep1")
rep2flx<-subset(flx,flx$replicate=="Rep2")
rep3flx<-subset(flx,flx$replicate=="Rep3")
rep4flx<-subset(flx,flx$replicate=="Rep4")
rep1cfm<-subset(cfm,cfm$replicate=="Rep1")
rep2cfm<-subset(cfm,cfm$replicate=="Rep2")
rep3cfm<-subset(cfm,cfm$replicate=="Rep3")
rep4cfm<-subset(cfm,cfm$replicate=="Rep4")
rep1cwt<-subset(cwt,cwt$replicate=="Rep1")
rep2cwt<-subset(cwt,cwt$replicate=="Rep2")
rep3cwt<-subset(cwt,cwt$replicate=="Rep3")
rep4cwt<-subset(cwt,cwt$replicate=="Rep4")
rep1flx.m<-mean(rep1flx$p1) 
rep2flx.m<-mean(rep2flx$p1)
rep3flx.m<-mean(rep3flx$p1)
rep4flx.m<-mean(rep4flx$p1)
rep.flx.m<-c(rep1flx.m,rep2flx.m,rep3flx.m,rep4flx.m)
rep1cfm.m<-mean(rep1cfm$p1)
rep2cfm.m<-mean(rep2cfm$p1)
rep3cfm.m<-mean(rep3cfm$p1)
rep4cfm.m<-mean(rep4cfm$p1)
rep.cfm.m<-c(rep1cfm.m,rep2cfm.m,rep3cfm.m,rep4cfm.m)
rep1cwt.m<-mean(rep1cwt$p1)
rep2cwt.m<-mean(rep2cwt$p1)
rep3cwt.m<-mean(rep3cwt$p1)
rep4cwt.m<-mean(rep4cwt$p1)
rep.cwt.m<-c(rep1cwt.m,rep2cwt.m,rep3cwt.m,rep4cwt.m)
mean.flx<-mean(flx$p1)
mean.cfm<-mean(cfm$p1)
mean.cwt<-mean(cwt$p1)
Mean<- c(mean.flx,mean.cfm,mean.cwt)
Treatment<-c("FLX","CFM","CWT")
se.flx<-(sd(flx$p1))/(sqrt(length(na.omit(flx$p1))))
se.cfm<-(sd(cfm$p1))/(sqrt(length(na.omit(cfm$p1))))
se.cwt<-(sd(cwt$p1))/(sqrt(length(na.omit(cwt$p1))))
Se<-c(se.flx,se.cfm,se.cwt)
Mean<-cbind(Treatment,Mean,Se)
mean<-as.data.frame(Mean)
mean$Mean<-as.numeric(mean$Mean)
mean$Se<-as.numeric(mean$Se)
mean$Treatment<-factor(mean$Treatment,levels = c("FLX","CFM","CWT"))
par(mar = c(3, 5, 2, 2))
x<-c(1:3)
plot(x,mean$Mean,type="b",lty=0, xlim=c(0.5,3.5), xlab = "",
     col=c("#FF3333", "#00CCCC","#009966"),
     ylim=c(0.01, 0.4), ylab="",cex = 1.8,las=2,
     xaxt='n',main="",pch = c(15, 16, 17))
title(main ="Defense ability",line=0.5,cex.main=1.6,font.main=1)
title(ylab ="Proportion of offspring",line = 3,cex.lab=1.6)
axis(1, at=c(1,2,3),labels = c("FLX","CFM","CWT"),cex.axis=1.4)
# legend(0.4,0.47,inset=0,y.intersp=1,legend = c("FLX","CFM","CWT"),pch = c(15, 16, 17),cex = 1.4, bty = "n",x.intersp=0.2,col=c("#FF3333", "#00CCCC","#009966"))
lines(x, mean$Mean, col="gray",lty=1)
errbar(x, mean$Mean,mean$Mean+mean$Se,mean$Mean-mean$Se,add=TRUE,
       errbar.col=c("#FF3333", "#00CCCC","#009966"),pch = c(15, 16, 17),
       col=c("#FF3333", "#00CCCC","#009966"),lwd=1.8)
points(jitter(rep(1, 4), -0.16, 0.16), rep.flx.m,pch = 15, cex = 1, col = "#FF6633") 
points(jitter(rep(2, 4), -0.15, 0.15), rep.cfm.m,pch = 16, cex = 1, col = "#00FFCC")
points(jitter(rep(3, 4), -0.15, 0.15), rep.cwt.m,pch = 17, cex = 1, col = "#009900")
segments(1.1,0.05,1.9,0.05, lwd=2, col="darkgray")
text(1.5,0.065,"p=0.007",cex = 1.3)
segments(2.1,0.05,2.9,0.05, lwd=2, col="darkgray")
text(2.5,0.065,"p=0.15",cex = 1.3)
segments(1.1,0.01,2.9,0.01, lwd=2, col="darkgray")
text(2,0.023,"p=0.46",cex = 1.3)

# Figure 2D, Offense
Offense$p2<-Offense$red.eyed/Offense$all
of<-na.omit(Offense)
flx<-subset(of,of$treatment=="FLX")
cfm<-subset(of,of$treatment=="CFM")
cwt<-subset(of,of$treatment=="CWT")
rep1flx<-subset(flx,flx$replicate=="rep1")
rep2flx<-subset(flx,flx$replicate=="rep2")
rep3flx<-subset(flx,flx$replicate=="rep3")
rep4flx<-subset(flx,flx$replicate=="rep4")
rep1cfm<-subset(cfm,cfm$replicate=="rep1")
rep2cfm<-subset(cfm,cfm$replicate=="rep2")
rep3cfm<-subset(cfm,cfm$replicate=="rep3")
rep4cfm<-subset(cfm,cfm$replicate=="rep4")
rep1cwt<-subset(cwt,cwt$replicate=="rep1")
rep2cwt<-subset(cwt,cwt$replicate=="rep2")
rep3cwt<-subset(cwt,cwt$replicate=="rep3")
rep4cwt<-subset(cwt,cwt$replicate=="rep4")
rep1flx.m<-mean(rep1flx$p2) 
rep2flx.m<-mean(rep2flx$p2)
rep3flx.m<-mean(rep3flx$p2)
rep4flx.m<-mean(rep4flx$p2)
rep.flx.m<-c(rep1flx.m,rep2flx.m,rep3flx.m,rep4flx.m)
rep1cfm.m<-mean(rep1cfm$p2)
rep2cfm.m<-mean(rep2cfm$p2)
rep3cfm.m<-mean(rep3cfm$p2)
rep4cfm.m<-mean(rep4cfm$p2)
rep.cfm.m<-c(rep1cfm.m,rep2cfm.m,rep3cfm.m,rep4cfm.m)
rep1cwt.m<-mean(rep1cwt$p2)
rep2cwt.m<-mean(rep2cwt$p2)
rep3cwt.m<-mean(rep3cwt$p2)
rep4cwt.m<-mean(rep4cwt$p2)
rep.cwt.m<-c(rep1cwt.m,rep2cwt.m,rep3cwt.m,rep4cwt.m)
mean.flx<-mean(flx$p2)
mean.cfm<-mean(cfm$p2)
mean.cwt<-mean(cwt$p2)
Mean<- c(mean.flx,mean.cfm,mean.cwt)
Treatment<-c("FLX","CFM","CWT")
se.flx<-(sd(flx$p2))/(sqrt(length(na.omit(flx$p2))))
se.cfm<-(sd(cfm$p2))/(sqrt(length(na.omit(cfm$p2))))
se.cwt<-(sd(cwt$p2))/(sqrt(length(na.omit(cwt$p2))))
Se<-c(se.flx,se.cfm,se.cwt)
Mean<-cbind(Treatment,Mean,Se)
mean<-as.data.frame(Mean)
mean$Mean<-as.numeric(mean$Mean)
mean$Se<-as.numeric(mean$Se)
mean$Treatment<-factor(mean$Treatment,levels = c("FLX","CFM","CWT"))
par(mar = c(3, 5, 2, 2))
x<-c(1:3)
plot(x,mean$Mean,type="b",lty=0, xlim=c(0.5,3.5), xlab = "",
     col=c("#FF3333", "#00CCCC","#009966"),
     ylim=c(0.75, 0.97), ylab="",cex = 1.8,las=2,
     xaxt='n',main="",pch = c(15, 16, 17))
title(main ="Offense ability",line=0.5,cex.main=1.6,font.main=1)
title(ylab ="Proportion of offspring",line = 3,cex.lab=1.6)
axis(1, at=c(1,2,3),labels = c("FLX","CFM","CWT"),cex.axis=1.4)
legend(0.33,0.983,inset=0,y.intersp=0.7,legend = c("FLX","CFM","CWT"),
       pch = c(15, 16, 17),cex = 1.4, bty = "n",x.intersp=0.7,col=c("#FF3333", "#00CCCC","#009966"))
lines(x, mean$Mean, col="gray",lty=1)
errbar(x, mean$Mean,mean$Mean+mean$Se,mean$Mean-mean$Se,add=TRUE,
       errbar.col=c("#FF3333", "#00CCCC","#009966"),pch = c(15, 16, 17),
       col=c("#FF3333", "#00CCCC","#009966"),lwd=1.8)
points(jitter(rep(1, 4), -0.16, 0.16), rep.flx.m,pch = 15, cex = 1, col = "#FF6633") 
points(jitter(rep(2, 4), -0.15, 0.15), rep.cfm.m,pch = 16, cex = 1, col = "#00FFCC")
points(jitter(rep(3, 4), -0.15, 0.15), rep.cwt.m,pch = 17, cex = 1, col = "#009900")
segments(1.1,0.78,1.9,0.78, lwd=2, col="darkgray")
text(1.5,0.787,"p=0.07",cex = 1.3)
segments(2.1,0.78,2.9,0.78, lwd=2, col="darkgray")
text(2.5,0.787,"p=0.28",cex = 1.3)
segments(1.1,0.76,2.9,0.76, lwd=2, col="darkgray")
text(2,0.766,"p=0.765",cex = 1.3)


### Correlation analyses: Body size (Thorax size) ~ sperm
# body size ~ Defense (p1)
str(Defense)
d<-Defense
rep1<-subset(d,d$replicate=="Rep1")
rep2<-subset(d,d$replicate=="Rep2")
rep3<-subset(d,d$replicate=="Rep3")
rep4<-subset(d,d$replicate=="Rep4")
rep1flx<-subset(rep1,rep1$treatment=="FLX")
rep1cfm<-subset(rep1,rep1$treatment=="CFM")
rep1cwt<-subset(rep1,rep1$treatment=="CWT")
rep2flx<-subset(rep2,rep2$treatment=="FLX")
rep2cfm<-subset(rep2,rep2$treatment=="CFM")
rep2cwt<-subset(rep2,rep2$treatment=="CWT")
rep3flx<-subset(rep3,rep3$treatment=="FLX")
rep3cfm<-subset(rep3,rep3$treatment=="CFM")
rep3cwt<-subset(rep3,rep3$treatment=="CWT")
rep4flx<-subset(rep4,rep4$treatment=="FLX")
rep4cfm<-subset(rep4,rep4$treatment=="CFM")
rep4cwt<-subset(rep4,rep4$treatment=="CWT")
rep1flx.m<-mean(rep1flx$p1)
rep1cfm.m<-mean(rep1cfm$p1)
rep1cwt.m<-mean(rep1cwt$p1)
rep2flx.m<-mean(rep2flx$p1)
rep2cfm.m<-mean(rep2cfm$p1)
rep2cwt.m<-mean(rep2cwt$p1)
rep3flx.m<-mean(rep3flx$p1)
rep3cfm.m<-mean(rep3cfm$p1)
rep3cwt.m<-mean(rep3cwt$p1)
rep4flx.m<-mean(rep4flx$p1,na.rm=TRUE)
rep4cfm.m<-mean(rep4cfm$p1,na.rm=TRUE)
rep4cwt.m<-mean(rep4cwt$p1,na.rm=TRUE)

mean<-matrix(c("1","FLX","0.1437236","1","CFM","0.1269129","1","CWT","0.1590302","2","FLX","0.378446","2","CFM","0.07290101","2","CWT","0.07381965",
               "3","FLX","0.2376111","3","CFM","0.1481527","3","CWT","0.1864585","4","FLX","0.1078267","4","CFM","0.06646948","4","CWT","0.1138599"),ncol = 3,byrow = TRUE)
colnames(mean)<-c("replicate","treatment","p1")
def.m<-as.data.frame(mean)

MaleThoraxsize <- read_excel("~/Desktop/MaleThoraxsize.xlsx")
size<-as.data.frame(MaleThoraxsize)
for(x in 1:2)size[,x]<-as.factor(size[,x])
def.m$size<-size$size
ss<-def.m # sperm and size

Offense$p2<-Offense$red.eyed/Offense$all
of<-na.omit(Offense)
flx<-subset(of,of$treatment=="FLX")
cfm<-subset(of,of$treatment=="CFM")
cwt<-subset(of,of$treatment=="CWT")
rep1flx<-subset(flx,flx$replicate=="rep1")
rep2flx<-subset(flx,flx$replicate=="rep2")
rep3flx<-subset(flx,flx$replicate=="rep3")
rep4flx<-subset(flx,flx$replicate=="rep4")
rep1cfm<-subset(cfm,cfm$replicate=="rep1")
rep2cfm<-subset(cfm,cfm$replicate=="rep2")
rep3cfm<-subset(cfm,cfm$replicate=="rep3")
rep4cfm<-subset(cfm,cfm$replicate=="rep4")
rep1cwt<-subset(cwt,cwt$replicate=="rep1")
rep2cwt<-subset(cwt,cwt$replicate=="rep2")
rep3cwt<-subset(cwt,cwt$replicate=="rep3")
rep4cwt<-subset(cwt,cwt$replicate=="rep4")
rep1flx.m<-mean(rep1flx$p2)
rep2flx.m<-mean(rep2flx$p2)
rep3flx.m<-mean(rep3flx$p2)
rep4flx.m<-mean(rep4flx$p2)
rep1cfm.m<-mean(rep1cfm$p2)
rep2cfm.m<-mean(rep2cfm$p2)
rep3cfm.m<-mean(rep3cfm$p2)
rep4cfm.m<-mean(rep4cfm$p2)
rep1cwt.m<-mean(rep1cwt$p2)
rep2cwt.m<-mean(rep2cwt$p2)
rep3cwt.m<-mean(rep3cwt$p2)
rep4cwt.m<-mean(rep4cwt$p2)

mean2<-matrix(c("1","FLX","0.8595733","3","1","CFM","0.9009876","1","1","CWT","0.8466905","3","2","FLX","0.9166794","2","2","CFM","0.9338075","1","2","CWT","0.8683853","1",
                "3","FLX","0.7861751","1","3","CFM","0.9467075","2","3","CWT","0.9584476","2","4","FLX","0.8758849","1","4","CFM","0.9246373","3","4","CWT","0.8087071","2"),ncol = 4,byrow = TRUE)
colnames(mean2)<-c("replicate","treatment","p2","block")
off.m<-as.data.frame(mean2)
ss$p2<-off.m$p2
ss$treatment<-factor(ss$treatment,levels =c("FLX","CFM","CWT"))
ss$p1<-as.numeric(ss$p1)
ss$p2<-as.numeric(ss$p2)

ggscatter(ss, x = "size", y = "p1",
          color = "treatment", shape = "treatment",size = 3,
          palette = c("#FF3333", "#00CCCC", "#009966"),legend = c(0.1,0.85), legend.title = "",
          title="Thorax size vs defense ability",
          add = "reg.line",
          add.params = list(color = "blue", fill = "lightgray"),
          ylab="Defense ability",xlab="Thorax size in mm",
          font.x = c(16,  "black"),font.y = c(16,  "black"),
          conf.int = TRUE,cor.coef = TRUE,
            cor.coeff.args = list(method = "pearson", label.x = 0.82),cor.coef.size = 5)

ggscatter(ss, x = "size", y = "p2",
          color = "treatment", shape = "treatment",size = 3,
          palette = c("#FF3333", "#00CCCC", "#009966"),legend = c(0.1,0.18), legend.title = "",
          title="Thorax size vs offense ability",
          add = "reg.line",
          add.params = list(color = "blue", fill = "lightgray"),
          ylab="Offense ability",xlab="Thorax size in mm",
          font.x = c(16,  "black"),font.y = c(16,  "black"),
          conf.int = TRUE,cor.coef = TRUE,
          cor.coeff.args = list(method = "pearson", label.x = 0.82),cor.coef.size = 5)


#________________________________________________________end.
