Functions for ggplot themes


themeLME<-function(legPos) {
  theme_bw()+
    theme(axis.text=element_text(size=14,color="black"),
          axis.title=element_text(size=16,face="bold"),
          legend.position=legPos, 
          legend.text=element_text(size=14),
          legend.title=element_text(size=16),
          legend.direction="vertical",
          plot.title=element_text(size=22,face="bold", hjust=0.5), 
          plot.background=element_blank(),
          panel.grid.major=element_blank(),
          panel.grid.minor=element_blank(),
          axis.line=element_line(color="black")
    )
}

themeHist<-function(fontSize) {
  theme_bw()+
    theme(axis.text=element_text(size=fontSize,color="black"),
          axis.title=element_text(size=(fontSize+2),face="bold"),
          legend.position="none", 
          legend.text=element_text(size=(fontSize-2)),
          legend.title=element_text(size=fontSize),
          legend.direction="vertical",
          plot.title=element_text(size=(fontSize+4),face="bold", hjust=0.5), 
          plot.background=element_blank(),
          panel.grid.major=element_blank(),
          panel.grid.minor=element_blank(),
          panel.border=element_blank(),
          axis.line=element_line(color="black")
    )
}


Function to calculate Spearman or Pearson correlations


## create matrix to hold values

spear<-matrix(ncol=8, nrow=nr)

## set value of y to 1
y<-1

## loop through the LMEs

for (lme in unique(lmes$LME)) {
  xd<-subset(lmes, LME==lme)
  spear[y,1]<-lme
  ### change method to pearson if required
  m<-"spearman" ## or
  #m<-"pearson"
  spear[y,2]<-cor.test(xd$Weight, xd$year, method=m)$estimate
  spear[y,3]<-cor.test(xd$FRic, xd$year, method=m)$estimate
  spear[y,4]<-cor.test(xd$FEve, xd$year, method=m)$estimate
  spear[y,5]<-cor.test(xd$FDiv, xd$year, method=m)$estimate
  spear[y,6]<-cor.test(xd$FRic, xd$year, method=m)$estimate
  spear[y,7]<-cor.test(xd$FEve, xd$Weight, method=m)$estimate
  spear[y,8]<-cor.test(xd$FDiv, xd$Weight, method=m)$estimate
  y<-y+1
}

## convert to data frame

spear<-data.frame(spear)
colnames(spear)<-c("LME","WtxYr","FRxYr","FExYr", "FDxYr", "FRxWt","FExWt", "FDxWt")


Function to calculate standarised slopes


## create matrix to hold values

library(lm.beta)

stSlopes<-matrix(ncol=4, nrow=nr)

## set value of y to 1
y<-1

## loop through the LMEs

for (lme in unique(lmes$LME)) {
  xd<-subset(lmes, LME==lme)
  stSlopes[y,1]<-lme
  stSlopes[y,2]<-lm.beta(lm(xd$FRic~xd$year))$standardized.coefficients[[2]]
  stSlopes[y,3]<-lm.beta(lm(xd$FEve~xd$year))$standardized.coefficients[[2]]
  stSlopes[y,4]<-lm.beta(lm(xd$FDiv~xd$year))$standardized.coefficients[[2]]
  y<-y+1
}

## convert to data frame

stSlopes<-data.frame(stSlopes)
colnames(stSlopes)<-c("LME","FRsl","FEsl","FDsl")


Code to produce scatter plots and density histograms


### scatter plots

p<-ggplot(spA, aes(x=wtsl, y=FRsl))+geom_point(size=3, colour="grey50")
p<-p+themeLME("none")+xlab("")+ylab("Functional richness")
p<-p+geom_vline(xintercept=0, linetype=2)+ylim(-1, 1)+xlim(-1, 1)
p<-p+ggtitle("Actinopterygii")+geom_hline(yintercept=0, linetype=2)
p<-ggMarginal(p, type="histogram", bins=15, fill="darkgreen", alpha=0.7)

### density histograms

h<-ggplot(data=spAd, aes(spAd$FRsl))+geom_histogram(bins=20, col="gray30", fill="grey70", alpha=0.5)
h<-h+labs(x="",y="Functional richness")+ylim(0, 35)
h<-h+geom_density(alpha=0.5, fill="darkgreen", aes(y=nsp*..density..) )+xlim(-1.5, 1.5)
h<-h+themeHist(14)+ggtitle("Actinopterygii")+geom_vline(xintercept=0, linetype=3)


Standardised slopes & histograms by catch




Standardised slopes & histograms by taxonomic richness



Density histograms