Load packages:

#Install vegan and tidyverse if you haven't got it already
if(!require("vegan")) install.packages("vegan")
if(!require("tidyverse")) install.packages("tidyverse")
if(!require("ggplot2")) install.packages("ggplot2")
if(!require("dplyr")) install.packages("dplyr")

#load vegan and tidyverse
library("vegan")
library("tidyverse")
library("ggplot2")
library("dplyr")

Read in an original fyke data file with CPUE calcs

Create your working NMDS df

Uses the siteID column you created earlier and, if applicable, takes the mean CPUE for any species that may have been sampled from veg zones with greater than one site per veg zone

Calculate dist matrix for fish.

#bring in your grouping spreadsheets to be used for color-coding when plotting
fyke_unit = read.csv("UMSEAS-SNWR-2022_fykeNMDS_unit.csv", header = T)
fyke_veg = read.csv("UMSEAS-SNWR-2022_fykeNMDS_veg.csv", header = T)
fyke_month = read.csv("UMSEAS-SNWR-2022_fykeNMDS_month.csv", header = T)

#converting cpue data from 2020fykeNMDS.csv to rel abundance and calculating the bray-curtis diss dist matrix
fyke_rel = decostand(fyke, method = "total")
fyke_distmat = vegdist(fyke, method = "bray")

#read in the dist matrix from prior function as a matrix and save back to your working directory
fyke_distmat = as.matrix(fyke_distmat, labels = T)
write.csv(fyke_distmat, "UMSEAS-SNWR-2022_fykeNMDS_braydistancematrix.csv")

Perform NMDS and get MDS data for plotting.

## Run 0 stress 0.1262625 
## Run 1 stress 0.136691 
## Run 2 stress 0.1251096 
## ... New best solution
## ... Procrustes: rmse 0.04451511  max resid 0.1110925 
## Run 3 stress 0.1355399 
## Run 4 stress 0.1270204 
## Run 5 stress 0.1270202 
## Run 6 stress 0.1251095 
## ... New best solution
## ... Procrustes: rmse 0.0003098983  max resid 0.001127404 
## ... Similar to previous best
## Run 7 stress 0.1251094 
## ... New best solution
## ... Procrustes: rmse 0.0005653077  max resid 0.001789839 
## ... Similar to previous best
## Run 8 stress 0.1355419 
## Run 9 stress 0.1262628 
## Run 10 stress 0.1251137 
## ... Procrustes: rmse 0.0205621  max resid 0.09374683 
## Run 11 stress 0.1251142 
## ... Procrustes: rmse 0.02059309  max resid 0.0933837 
## Run 12 stress 0.1251137 
## ... Procrustes: rmse 0.02060705  max resid 0.09386643 
## Run 13 stress 0.1251139 
## ... Procrustes: rmse 0.02054685  max resid 0.09344043 
## Run 14 stress 0.1270204 
## Run 15 stress 0.1351849 
## Run 16 stress 0.1251139 
## ... Procrustes: rmse 0.02063774  max resid 0.09524371 
## Run 17 stress 0.1270202 
## Run 18 stress 0.1251139 
## ... Procrustes: rmse 0.02063361  max resid 0.09381211 
## Run 19 stress 0.1339748 
## Run 20 stress 0.1275336 
## *** Best solution repeated 1 times
## 
## Call:
## metaMDS(comm = fyke_distmat, distance = "bray", k = 3, trymax = 500,      wascores = TRUE, maxit = 999) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     fyke_distmat 
## Distance: user supplied 
## 
## Dimensions: 3 
## Stress:     0.1251094 
## Stress type 1, weak ties
## Best solution was repeated 1 time in 20 tries
## The best solution was from try 7 (random start)
## Scaling: centring, PC rotation 
## Species: scores missing

#time to plot

## Saving 7 x 5 in image

## Saving 7 x 5 in image

## Saving 7 x 5 in image

#run an ADONIS code, that is, a PERMANOVA test, on the code for month

adonis2(fyke_rel ~ Month, data = site.scores, permutations = 999, method = "bray")
Fish2022ADONISmonth = adonis2(fyke_rel ~ Month, data = site.scores, permutations = 999, method = "bray", by = NULL)
print(Fish2022ADONISmonth)
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = fyke_rel ~ Month, data = site.scores, permutations = 999, method = "bray", by = NULL)
##          Df SumOfSqs      R2     F Pr(>F)  
## Model     4   1.8094 0.17966 1.533  0.023 *
## Residual 28   8.2621 0.82034               
## Total    32  10.0716 1.00000               
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

UNIT

## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values (`geom_path()`).

## Saving 7 x 5 in image
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values (`geom_path()`).
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values (`geom_path()`).

## Saving 7 x 5 in image
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values (`geom_path()`).
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values (`geom_path()`).

## Saving 7 x 5 in image
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values (`geom_path()`).

#run an ADONIS code, that is, a PERMANOVA test, on the code for unit.

adonis2(fyke_rel ~ Unit, data = site.scores, permutations = 999, method = "bray")
Fish2022ADONISunit = adonis2(fyke_rel ~ Unit, data = site.scores, permutations = 999, method = "bray", by = NULL)
print(Fish2022ADONISunit)
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = fyke_rel ~ Unit, data = site.scores, permutations = 999, method = "bray", by = NULL)
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     4   2.3939 0.23769 2.1826  0.002 **
## Residual 28   7.6777 0.76231                 
## Total    32  10.0716 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

VEG

## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Warning: Removed 3 rows containing missing values (`geom_path()`).

## Saving 7 x 5 in image
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Warning: Removed 3 rows containing missing values (`geom_path()`).
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Warning: Removed 3 rows containing missing values (`geom_path()`).

## Saving 7 x 5 in image
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Warning: Removed 3 rows containing missing values (`geom_path()`).
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Warning: Removed 3 rows containing missing values (`geom_path()`).

## Saving 7 x 5 in image
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Warning: Removed 3 rows containing missing values (`geom_path()`).
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = fyke_rel ~ Veg, data = site.scores, permutations = 999, method = "bray", by = NULL)
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     5   1.9970 0.19828 1.3355  0.088 .
## Residual 27   8.0746 0.80172                
## Total    32  10.0716 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1