UWA logoMaterial to support teaching in Environmental Science at The University of Western Australia

Units ENVT3361, ENVT4461, and ENVT5503

Learning Outcomes:

  1. Students are able to appropriately analyse data from an eDNA field sampling campaign;
  2. Students are able to interpret and understand statistical outputs from the R package vegan.
library(vegan)          # ecological multivariate analysis
library(flextable)      # publication-quality tables
  set_flextable_defaults(theme_fun = "theme_booktabs", font.size = 10)
library(officer)        # functions to support some flextable options
library(stringr)        # character string manipulation
library(viridis)        # colourblind-friendly colour palettes
library(scatterplot3d)  # used for 3D ordination plots
library(TeachingDemos)  # used for shadowtext() function

Import the data

Notes:

  1. This code imports the data from a file where the samples are rows and the species are columns.
  2. We convert the column Site to a factor (categorical information in R)
git <- "https://github.com/Ratey-AtUWA/Learn-R-web/raw/refs/heads/main/"
eDNA2022 <- read.csv(file = paste0(git,"eDNA2022.csv"))
eDNA2022$Site <- as.factor(eDNA2022$Site)

If you have species as rows and samples as columns, we will need to do a bit of wrangling!

speciesAsRows <- read.csv(file = paste0(git,"speciesAsRows.csv"), row.names = 1)
speciesAsRows <- rbind(str_remove(colnames(speciesAsRows), "X"),
                       speciesAsRows)
row.names(speciesAsRows)[1] <- "SiteID"
speciesAsRows <- as.matrix(speciesAsRows) # convert to matrix for next step
speciesAsRows <- t(speciesAsRows) # t() means transpose
row.names(speciesAsRows) <- NULL
eDNA2022 <- as.data.frame(speciesAsRows)
eDNA2022$Site <- as.factor(eDNA2022$Site)

List of all the species and their common names

altnames <- 
  c("MosqFish", "Goby", "Mullet", "Bream", "BnjoFrog", "GToadlet", "MlrdDuck",
    "SpotDove", "SwampHen", "Crake", "WFHeron", "Lorikeet", "Cattle", "Sheep",
    "Mouse", "Rat", "HydroidB", "HydroidO", "BwnJelly", "MoonJely", "Dog")
names_table <- 
  data.frame(Taxonomic = gsub("."," ",colnames(eDNA2022[,6:26]), fixed = T),
             Common=c("Mosquito Fish","Bridled Goby","Gray Mullet","Black Bream",
                      "Western Banjo Frog", "Gunther's Toadlet", "Mallard Duck",
                      "Spotted Turtle Dove", "Purple Swamphen", "Spotted Crake",
                      "White Faced Heron", "Rainbow Lorikeet", "Cattle", "Sheep",
                      "Mouse", "Rat", "HydroidB", "HydroidO", "Brown Jellyfish",
                      "Moon Jellyfish", "Domestic Dog"),
             Abbreviated=altnames)
flextable(names_table,cwidth = c(3,2,2)) |> italic(j=1, italic=T) |> bold(bold=T, part="header") |> 
  padding(padding=2, part="all") |> 
  set_caption(caption="Table 1: Taxonomic, common, and abbreviated names for the species detected by eDNA sampling and analysis at Ashfield Flats in 2022.", align_with_table = F, fp_p=fp_par(text.align = "left", padding.bottom = 6))
Table 1: Taxonomic, common, and abbreviated names for the species detected by eDNA sampling and analysis at Ashfield Flats in 2022.

Taxonomic

Common

Abbreviated

Gambusia holbrooki

Mosquito Fish

MosqFish

Arenigobius bifrenatus

Bridled Goby

Goby

Mugil cephalus

Gray Mullet

Mullet

Acanthopagrus butcheri

Black Bream

Bream

Limnodynastes dorsalis

Western Banjo Frog

BnjoFrog

Pseudophryne guentheri

Gunther's Toadlet

GToadlet

Anas platyrhynchos

Mallard Duck

MlrdDuck

Spilopelia chinensis

Spotted Turtle Dove

SpotDove

Porphyrio porphyrio

Purple Swamphen

SwampHen

Porzana tabuensis

Spotted Crake

Crake

Egretta novaehollandiae

White Faced Heron

WFHeron

Trichoglossus rubritorquis

Rainbow Lorikeet

Lorikeet

Bos taurus

Cattle

Cattle

Ovis sp

Sheep

Sheep

Mus musculus

Mouse

Mouse

Rattus sp

Rat

Rat

Blackfordia polytentaculata

HydroidB

HydroidB

Obelia bidentata

HydroidO

HydroidO

Phyllorhiza punctata

Brown Jellyfish

BwnJelly

Aurelia sp

Moon Jellyfish

MoonJely

Canis lupus familiaris

Domestic Dog

Dog

 

We need to make a subset of our imported data which contains just the species presence/absence information (not sample IDs, sites, reps). We also overwrite the original column names with abbreviated names for better visualisation later.

allSpecies <- eDNA2022[,6:26] # not columns 1:5 which are sample IDs etc.
colnames(allSpecies) <- names_table[,3]

Difference and Similarity measures

We start by assessing how similar or different our samples are, and to do this we need to set up a resemblance matrix. Ideally our 4 replicated samples are more similar to one another than any other samples, but with field sampling this is not always the case. To determine similarity we need to use a similarity measure.

Similarity Measures:

A similarity measure is conventionally defined to take values between 0 and 100% (or 0 and 1) with the ends of the range representing the extreme possibilities:

  • Similarity = 100% (or 1) if two samples are totally similar
  • Similarity = 0 if two samples are totally dissimilar

What constitutes total similarity, and particularly total dissimilarity, of two samples depends on the specific criteria that are adopted. For example most biologists would feel that similarity should equal zero when two samples have no species in common, and similarity should equal 100% if two samples have identical entries. Similarities are calculated between every pair of samples creating a similarity matrix. There are a number of similarity measures (ways to calculate similarity) which can be used to create this matrix.

Bray Curtis and Jaccard are two different similarity measures we can use; both are commonly used in ecology and have desirable properties. Similarity is calculated based on the taxa that samples have in common (or not). Similarity takes a value of 0 if two samples have no species in common and joint absences have no effect (i.e. a taxon jointly absent in two samples does not contribute to its similarity). The difference between them is that Bray Curtis will use abundance information (where it is present) in assessing similarity, whereas Jaccard converts abundances to binary (i.e. 0s and 1s, with 1 representing presence and 0 meaning absence) to assess similarity. In our case it does not matter much which we use, since we have already converted to binary, so let’s proceed with Jaccard.

💡 Ecological Question: If two samples have no taxa present each,
does this make them more similar to one another? 💡

 

To assess similarity in the vegan R package carry out the following steps. The output shows the first few rows and columns of the dissimilarity matrix; each number represents the Jaccard distance between samples based on the presence or absence of species.

AF_diss_all <- vegdist(allSpecies, 
                           distance = "jaccard") # default is distance = "bray"

# just look at the top corner of the dissimilarity matrix:
txt0 <- capture.output(print(AF_diss_all, digits=3)); txt0[1:10]; rm(txt0)
##         1      2      3      4      5      6      7      8      9     10     11     12     13     14     15     16     17     18     19     20     21     22     23
## 2  0.0769
## 3  0.2727 0.1667
## 4  0.0769 0.0000 0.1667
## 5  0.3333 0.3846 0.6364 0.3846
## 6  0.3333 0.3846 0.6364 0.3846 0.0000
## 7  0.3333 0.3846 0.6364 0.3846 0.0000 0.0000
## 8  0.2727 0.3333 0.6000 0.3333 0.0909 0.0909 0.0909
## 9  0.8000 0.6364 0.5556 0.6364 0.6000 0.6000 0.6000 0.7778
## 10 0.6000 0.6364 0.5556 0.6364 0.6000 0.6000 0.6000 0.7778 0.2500
## 11 0.8000 0.6364 0.5556 0.6364 0.8000 0.8000 0.8000 1.0000 0.2500 0.2500

Ordinations

Next we will construct our ordination, which is a procedure to visualise the similarity matrix by simplifying (scaling) it into fewer dimensions than our original data. This reduction of dimensions is useful as the original data will have as many dimensions as there are species, which is impossible to visualise! There are two different types of ordination that we will use: Multi-Dimensional Scaling (nmMDS) and Principal Coordinates Analysis (PCO). If you have previously used PCA (Principal Component Analysis), you may be familiar with ordination methods. PCA is useful for environmental datasets; these are most often represented by a similarity matrix derived via use of Euclidean Distance as a measure of similarity. PCA is more suited to environmental variables because of the type of data; there are no large blocks of zeros (usually) and it is no longer necessary to select a similarity measure that ignores joint absences. When looking at ordination plots in general you should note that sample points that are spatially located together share common characteristics and sample points that are located distant from one another share less characteristics. This holds whether we are working with environmental variables (pH, EC, moisture content, heavy metals etc.) or species abundances.

The main difference between MDS and PCO relates to the underlying use of the resemblance matrix.

MDS takes the similarity measures calculated by Jaccard or Bray Curtis and ranks them in order. Thus pairs are samples are ranked according to highest to lowest similarity and are thus plotted in multidimensional space. Since it uses the rank and not the actual values, it's commonly called "non-[para]metric" multi-dimensional scaling, or nmMDS.

PCO takes the actual values of the underlying measure of similarity and plots pairs of samples in multi-dimensional space. It will then look for the direction (vector) of greatest variance in the multi-dimensional "cloud" of points, which becomes the first PCO dimension. Successive dimensions explain the most possible remaining variance, until all variance is accounted for. So, many PCO dimensions are possible, but usually only the first 2 or 3 are useful for visualizing our data.

For both types of ordinations the software attempts to preserve the similarity of each pair of samples. While preserving similarity is feasible in multidimensional space. In order for us to view the ordination, however, we see it displayed in 2 or 3 dimensions, so the software attempts to preserve as much as is feasible of the sample pair similarity as possible in 2-3 dimensions. In practice, we need to consider how much the 2- dimensional view is a true representation of the multidimensional data set. In nmMDS we use the stress value to evaluate this. In PCO we instead draw plots with various axis combinations, e.g. axis 1 vs. axis 2, axis 2 vs. axis 3 and so on. The amount of variance explained in each PCO dimension is called the eigenvalue, and the output will include these values; the greater the eigenvalue, the more useful a PCO dimension is. In practice, much of the variability is often explained in the first two axes (i.e. 2 dimensions) and we will limit our analysis to these axes for this dataset.

Non-metric multidimensional scaling (nmMDS)

In vegan this is done using the metaMDS() function. We need to specify an input matrix of just the species data (e.g. allSpecies which we made earlier). The default is to find 2 dimensions (k) using a Bray-Curtis dissimilarity matrix, but we can set these options to different values using k = and distance = options. The dissimilarity matrix is calculated within the metaMDS() function, so for consistency we set distance = "jaccard".

# default with 2 dimensions
AF_nmds_all <- metaMDS(allSpecies, distance = "jaccard")
# 3 dimensions for comparison
AF_nmds_all3 <- metaMDS(allSpecies, k=3, distance = "jaccard")
# show output for 2 nmMDS dimensions
AF_nmds_all
##
## Call:
## metaMDS(comm = allSpecies, distance = "jaccard")
##
## global Multidimensional Scaling using monoMDS
##
## Data:     allSpecies
## Distance: jaccard
##
## Dimensions: 2
## Stress:     0.1554552
## Stress type 1, weak ties
## Best solution was repeated 6 times in 20 tries
## The best solution was from try 6 (random start)
## Scaling: centring, PC rotation, halfchange scaling
## Species: expanded scores based on 'allSpecies'

The 2D stress value is ≃ 0.16. This can be interpreted as follows:

The stress value is relatively high as we are dealing with a large number of samples with lots of values of 0 or 1 in a 2 dimensional space, resulting in a challenging similarity environment.

💡 Check the stress value of the 3-D ordination. Is it lower than for 2-D? 💡

Remember that we made the AF_nmds_all3 object in the code block above:

AF_nmds_all3 <- metaMDS(allSpecies, k=3, distance = "jaccard", trace=0, 
                        try=10, trymax=100)

 

Figure 1(a) below shows the two-dimensional nmMDS plot ordination.

par(mfrow=c(1,2), mar=c(3,3,1,0.5), mgp=c(1.5,0.2,0), tcl=-0.2, font.lab=2)
palette(c("#000", viridis(nlevels(eDNA2022$Site), alpha=0.8),"#fff"))
plot(AF_nmds_all$points, pch=rep(21:25, l=nlevels(eDNA2022$Site))[eDNA2022$Site], 
     xlim = c(-1.6,2.2), ylim = c(1.2,-1.2), 
     bg=c(2:12)[eDNA2022$Site], 
     cex = 1.4, main = "")
text(AF_nmds_all, display = "species", col = 6, font=3, cex = 1)
mtext("(a)", 3, -1.2, adj=0.98, font=2, cex=1.4)
legend("bottomright", box.col="grey", bg="transparent", ncol=4, title = "Site", 
       legend=c(1:11), pch=c(rep(21:25, 2),21), 
       inset=0.01, pt.bg=c(2:12), pt.cex=1.4, cex=1.2)
Figure 1: (a) 2D NMDS ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022; (b) for reference, an Ashfield Flats map showing Site locations.

Figure 1: (a) 2D NMDS ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022; (b) for reference, an Ashfield Flats map showing Site locations.

 

You can play around with the code for Figure 1 to make additional changes; you won’t break it by trying, so play around as much as you like. You will notice that there are only 2 points visible for Site 2; this is because the points are overlaid on top of one another. Our eDNA analysis is are detecting only a few taxa, so some of the samples are very similar to one another. However we can generally see that there are sample groupings and that likely location has an effect (i.e. samples that are from the same location are closer together). We will test this statistically a little later.

We can also generate a form of 3-dimensional plot using the scatterplot3d() function from the scatterplot3d R package. Figures 2 and 3 show 3D plots for the 3-dimension NMDS generated above, with interactivity available in Figure 3.

palette(c("black", viridis(10))) # a viridis palette
par(mfrow=c(1,1), mar=c(3,3,2,0.5), mgp=c(1.5,0.2,0), tcl=0.2, font.lab=2)
s3d <- scatterplot3d(x=AF_nmds_all3$points[,1], y=AF_nmds_all3$points[,2],
              z=AF_nmds_all3$points[,3],
              xlab="MDS1", ylab="MDS2", zlab="MDS3",
              angle=25, mar=c(4,4,1,2), type="h", col.axis="darkgray", color="gray",
              pch=c(rep(21:25, 2),21)[eDNA2022$Site],
              bg=c(1:11)[eDNA2022$Site], cex.symbols=1.4)
legend(s3d$xyz.convert(1.7, 0.75, 1.6), legend = levels(eDNA2022$Site), title="Site", 
       bg="#ffffffa0", box.col="transparent", 
       pt.bg= c(1:11), pt.cex=1.4, col="gray", pch=c(rep(21:25, 2),21))
Figure 2: 3D NMDS ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022.

Figure 2: 3D NMDS ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022.

Figure 3: Interactive plot of the three-dimensional NMDS ordination, with different symbols showing sites numbered in the legend. Click-and-drag or swipe to rotate or zoom the plot.

 

Principal Coordinates (PCoA) Analysis

In the 'vegan' package we use Weighted classical multidimensional scaling, also known as Weighted Principal Coordinates Analysis. This is implemented using the function wcmdscale(). We plot the PCO output in Figure 4 below.

AF_pco_all <- wcmdscale(vegdist(allSpecies), eig=TRUE)
AF_pco_all
## Call: wcmdscale(d = vegdist(allSpecies), eig = TRUE)
##
##           Inertia Rank
## Total       8.169
## Real       10.272   15
## Imaginary  -2.103   21
##
## Results have 44 points, 15 axes
##
## Eigenvalues:
##  [1]  3.377  2.101  1.537  1.297  0.613  0.434  0.342  0.214  0.158  0.100  0.039  0.032  0.014  0.010  0.004 -0.006 -0.007 -0.009 -0.016 -0.021 -0.027 -0.035 -0.046
## [24] -0.052 -0.059 -0.074 -0.089 -0.094 -0.115 -0.134 -0.143 -0.163 -0.190 -0.212 -0.242 -0.371
##
## Weights: Constant
par(mfrow=c(1,1), mar=c(3,3,2,0.5), mgp=c(1.5,0.2,0), tcl=0.2, font.lab=2)
plot(AF_pco_all)
Figure 4: Basic principal coordinates analysis (PCoA) ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022.

Figure 4: Basic principal coordinates analysis (PCoA) ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022.

Figure 3 is a basic PCoA plot based on all species, showing the locations of individual samples by row number.

We can make more informative plots by using the information stored in the output object (AF_pco_all); for example, see Figure 5 below.

palette(c("black", plasma(10))) # a viridis palette
par(mfrow = c(1,1), mar = c(3,3,1,1), mgp = c(1.5,0.2,0), tcl = 0.25, 
    font.lab = 2, xpd=T)
s3d <- scatterplot3d(x=AF_pco_all$points[,1], y=AF_pco_all$points[,2], z=AF_pco_all$points[,3],
              xlab="Dim 1", ylab="Dim 2", zlab="Dim 3",
              angle=65, mar=c(4,4,3,2), type="h", col.axis="gray", color="#e0e0e0",
              pch=c(rep(21:25, 2),21)[eDNA2022$Site],
              bg=c(1:11)[eDNA2022$Site], cex.symbols=1.4,
              xlim=c(-0.6,0.4),ylim=c(-0.6,0.4),zlim=c(-0.3,0.4))
legend(s3d$xyz.convert(-1.5, 0.4, 0.7), legend = levels(eDNA2022$Site), title="Site", 
       horiz=TRUE, cex=0.8, bg="#ffffffa0", box.col="transparent", 
       pt.bg= c(1:11), pt.cex=1.1, col="gray", pch=c(rep(21:25, 2),21))
Figure 5: Principal coordinates (PCoA) ordination plot in 3D based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022.

Figure 5: Principal coordinates (PCoA) ordination plot in 3D based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022.

AF_pco_scores <- scores(AF_pco_all)

PERMANOVA

In order to test where location has a significant effect on the taxa at our field site we need to run an ANOVA. As we have multiple species to test together, however, we cannot run a basic ANOVA; instead, we must run a multivariate permutational ANOVA or PERMANOVA.

PERMANOVA shares some resemblance to ANOVA where they both measure the sum-of-squares within and between groups, and make use of an F test to compare within-group to between-group variance. However, while ANOVA calculates the significance of the result on assumption of normality, PERMANOVA draws tests for significance by comparing the actual F test result to that gained from random permutations of the objects between the groups. Moreover, whilst PERMANOVA tests for similarity based on a chosen distance measure, ANOVA tests for similarity of the group means.

We could calculate a univariate measure of diversity (a Simpson or Shannon index, for example) and run an ANOVA on that univariate value. In doing so, however, we are reducing the complex dataset to a simplified version and losing discrete information in the process. Remember that ANOVA also has an underlying assumption of normality. Generally speaking, however, biological species data are not normally distributed and thus do not satisfy this assumption. This makes applying a permutational ANOVA a much better option. Before the advent of computational capacity a permutational ANOVA would have taken quite some time to run; luckily now it only takes a matter of seconds to minutes depending on the complexity of the dataset.

In the vegan package, the adonis2() function implements Permutational Multivariate Analysis of Variance Using Distance Matrices (i.e. PERMANOVA).

We use a formula in the adonis2() function which expresses the community matrix (i.e. the columns of species presence or absence) as a function of a factor (in this case, Zone).

AF_permanova_all <- adonis2(eDNA2022[,6:26] ~ Zone, data = eDNA2022,
                            permutations = 9999)
AF_permanova_all
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 9999
##
## adonis2(formula = eDNA2022[, 6:26] ~ Zone, data = eDNA2022, permutations = 9999)
##          Df SumOfSqs      R2      F Pr(>F)
## Model     3   4.1623 0.50952 13.851  1e-04 ***
## Residual 40   4.0068 0.49048
## Total    43   8.1691 1.00000
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

P value by permutation =0.0001 indicating a significant effect of location on the community composition of our samples. Note that 9999 permutations were undertaken to arrive at this value.

The vegan R package doesn't (unfortunately) have a built-in function to generate pairwise comparisons which we would generally like to do after finding that H0 is rejected by the overall PERMANOVA. We'll write some code to do this below...

Checking equality of dispersion / variability in our groups

First we need to assess the variability in the groups, since unequal variability in groups can give a false positive error in PERMANOVA (Tidy Ecology, 2026). We measure multivariate variability in terms of the “beta dispersion”:

(bd0 <- betadisper(vegdist(eDNA2022[,6:26], method="jaccard"), eDNA2022$Zone))
##
##  Homogeneity of multivariate dispersions
##
## Call: betadisper(d = vegdist(eDNA2022[, 6:26], method = "jaccard"), group = eDNA2022$Zone)
##
## No. of Positive Eigenvalues: 17
## No. of Negative Eigenvalues: 19
##
## Average distance to median:
##  ChapmanN  ChapmanS Kitchener  Woolcock
##    0.4188    0.3292    0.3698    0.3327
##
## Eigenvalues for PCoA axes:
## (Showing 8 of 36 eigenvalues)
##  PCoA1  PCoA2  PCoA3  PCoA4  PCoA5  PCoA6  PCoA7  PCoA8
## 3.5662 2.4277 1.7743 1.6589 0.8326 0.6056 0.4872 0.3936
permutest(bd0, permutations = 9999)
##
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 9999
##
## Response: Distances
##           Df  Sum Sq  Mean Sq     F N.Perm Pr(>F)
## Groups     3 0.05874 0.019579 0.756   9999 0.5266
## Residuals 40 1.03598 0.025899

In the output above, the Permutation test for homogeneity of multivariate dispersions has p > 0.05 (marginally), so we can't reject our null hypothesis of equal dispersion in groups.

par(mar=c(3,3,1,1), mgp=c(1.7,0.3,0), tcl=0.25, lend=2, ljoin=1, font.lab=2)
plot(bd0, main="β-dispersion", col=mako(4, beg=0.2, end=0.7), 
     pch=c(16,15,18,17), cex=1.25)
Figure 6: Spider plot of beta dispersion showing centroids and points for each group in principal coordinates space.

Figure 6: Spider plot of beta dispersion showing centroids and points for each group in principal coordinates space.

The plot in Figure 6 shows that the dispersions are not exactly equal; the distances from the centroids to the surrounding points vary a bit for different groups, but we can't distinguish them statistically.

Next we show you how to calculate pairwise PERMANOVA comparisons manually...

Pairwise comparisons for PERMANOVA

Here's some annotated R code that will do the job:

# set it up
pairz <- combn(levels(eDNA2022$Zone), 2)

# do the pairwise comparisons by normal permanova
pw0 <- apply(pairz,2, FUN=function(pr) {
  keep <- which(eDNA2022$Zone %in% pr)
  Dsub <- as.matrix(eDNA2022[keep,6:26]) # same column numbers 6:26 as before 
  set.seed(31416) # set randomisation seed for "reproducible randomisation"
  a <- adonis2(Dsub ~ eDNA2022$Zone[keep], permutations=999, method="jaccard")
  c(group1=pr[1], group2=pr[2], Rsq=round(a$R2[1], 3), 
    pseudo_F=round(a$F[1], 2), p_raw=a$`Pr(>F)`[1])
 }
)
pw0 <- as.data.frame(t(pw0))
pw0[,3:5] <- apply(pw0[,3:5],2,as.numeric)
print(pw0, digits=3)
##      group1    group2   Rsq pseudo_F p_raw
## 1  ChapmanN  ChapmanS 0.361    12.41 0.001
## 2  ChapmanN Kitchener 0.258     7.65 0.001
## 3  ChapmanN  Woolcock 0.299     7.69 0.001
## 4  ChapmanS Kitchener 0.334    11.02 0.001
## 5  ChapmanS  Woolcock 0.463    15.54 0.001
## 6 Kitchener  Woolcock 0.282     7.08 0.001

Correct pairwise p-values for multiple comparisons (using Holm)

As we've seen with any multiple comparison test (e.g. any pairwise comparison, correlation matrices) the p-values need correcting for multiple comparisons. (You might remember that this is to adjust for the increased likelihood of a false positive occurring, when we run many tests simultaneously.) We'll use Holm's correction which you will have seen before for pairwise mean comparisons and so on.

# correct for multiple comparisons
pw0$p_holm <- p.adjust(pw0$p_raw, "holm")

# format into conventional pairwise p-value matrix
grps <- sort(unique(c(pw0$group1, pw0$group2))) # also = levels(eDNA2022$Zone)

pmat <- matrix(NA, nrow = length(grps), ncol = length(grps),
               dimnames = list(grps, grps))

for(i in seq_len(nrow(pw0))) {
  g1 <- pw0$group1[i]
  g2 <- pw0$group2[i]
  # pmat[g1, g2] <- pw0$p_holm[i]
  pmat[g2, g1] <- pw0$p_holm[i]
}
pmat <- pmat[2:nrow(pmat),1:(ncol(pmat)-1)] # remove rows/columns without p-vals
cat("P-values adjusted for multiple comparisons using Holm's correction:\n\n")
print(pmat, digits=3, na.print = "")
## P-values adjusted for multiple comparisons using Holm's correction:
##
##           ChapmanN ChapmanS Kitchener
## ChapmanS     0.006
## Kitchener    0.006    0.006
## Woolcock     0.006    0.006     0.006

In this example, we can reject the null hypothesis of equality for all the pairwise comparisons.
Remember that we needed to check for equal [beta] dispersion first, so we could be sure the pairwise differences were real!

Let's re-plot the nmMDS biplot to show points separated by Zone, and see if this is consistent (Figure 7):

par(mfrow=c(1,1), mar=c(3,3,1,0.5), mgp=c(1.5,0.2,0), tcl=-0.2, font.lab=2)
palette(c("#000", viridis(nlevels(eDNA2022$Zone), alpha=0.8),"#fff"))
plot(AF_nmds_all$points, pch=rep(21:25, l=nlevels(eDNA2022$Zone))[eDNA2022$Zone], 
     xlim = c(-1.6,2.2), ylim = c(1.2,-1.2), 
     bg=c(2:12)[eDNA2022$Zone], 
     cex = 1.4, main = "")
text(AF_nmds_all, display = "species", col = 2, font=3, cex = 1)
legend("bottomright", box.col="grey", bg="transparent", ncol=2, title = "Zone", 
       legend=levels(eDNA2022$Zone), pch=rep(21:25, l=nlevels(eDNA2022$Zone)), 
       inset=0.01, pt.bg=c(1:nlevels(eDNA2022$Zone))+1, pt.cex=1.4, cex=1.2)
Figure 7: 2D nmMDS ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022; observations are identified by Zone.

Figure 7: 2D nmMDS ordination plot based on Jaccard (dis)similarities in eDNA species presence-absence data for water sampled from Ashfield Flats in 2022; observations are identified by Zone.

 

Figure 7 shows some overlap between groups, which might at first seem inconsistent with the (pairwise) PERMANOVA. We need to remember, though, that the PERMANOVA compares groups using the whole multivariate space, whereas the nmMDS plot in Figure 7 shows only 2 dimensions.

 

Additional options for vegan ordination plots

If we have factors (categories) in our data it's useful to show these using our multivariate ordination plots. We can categorise the Sites based on their broad site location (the Zone category shown in Figure 1(b)).

We can then add geometric shapes on our ordination plots to emphasise the positions of different groups (i.e. factor levels) within the ordination dimensions' space (Figure 8).

Click to show code

These plot features are really only applicable to two-dimensional (x-y) plots. 3D geometric shapes could be hard to interpret on a 3D plot!

# plot (a) ellipses
par(mar=c(3,3,1,1), mgp=c(1.7,0.3,0), tcl=0.25, lend=2, ljoin=1, font.lab=2,
    mfrow=c(2,2))
palette(c("black", inferno(nlevels(eDNA2022$Zone), end=0.75),"white"))
plot(AF_nmds_all, display="sites", cex=1.4, xlim=c(-1.65,1.65), ylim=c(-1.85,1.25))
mtext("(a)", side=3, line=-1.6, adj=0.03, cex=1.4)
points(AF_nmds_all$points, col=6, bg=c(2:5)[eDNA2022$Zone], 
       pch=c(21:25)[eDNA2022$Zone], cex=1.8)
ordiellipse(AF_nmds_all, groups=eDNA2022$Zone, col=c(2:5), lwd=2, 
            kind = "sd", conf=0.75)
shadowtext(tapply(AF_nmds_all$points[,1], eDNA2022$Zone, mean),
           tapply(AF_nmds_all$points[,2], eDNA2022$Zone, mean),
           labels=levels(eDNA2022$Zone), cex=1.2, col=c(2:5), bg=6, r=0.2)
legend("bottomright", ncol=1, bty="n", legend=levels(eDNA2022$Zone), col=c(2:5), 
       pt.bg=c(2:5), lty=1, lwd=2, pch=c(21:24), pt.cex=1.8, cex=1.2)

# plot (b) convex hulls
palette(c("black", cividis(nlevels(eDNA2022$Zone), end=0.75),"white"))
plot(AF_nmds_all, display="sites", cex=1.4, xlim=c(-1.65,1.65), ylim=c(-1.85,1.25))
ordihull(AF_nmds_all, groups=eDNA2022$Zone, 
         col=c(2:5), lwd=2, label=F)
points(AF_nmds_all$points, col=6, bg=c(2:5)[eDNA2022$Zone], 
       pch=c(21:25)[eDNA2022$Zone], cex=1.8)
shadowtext(tapply(AF_nmds_all$points[,1], eDNA2022$Zone, mean),
           tapply(AF_nmds_all$points[,2], eDNA2022$Zone, mean),
           labels=levels(eDNA2022$Zone), cex=1.2, col=c(2:5), bg=6, r=0.2)
mtext("(b)", side=3, line=-1.6, adj=0.03, cex=1.4)
legend("bottomright", ncol=1, bty="n", legend=levels(eDNA2022$Zone), col=c(2:5), 
       pt.bg=c(2:5), lty=1, lwd=2, pch=c(21:24), pt.cex=1.8, cex=1.2)

# (c) spiders
palette(c("black", viridis(nlevels(eDNA2022$Zone), end=0.75),"white"))
plot(AF_nmds_all, display="sites", cex=0.8, xlim=c(-1.65,1.65), ylim=c(-1.85,1.25))
mtext("(c)", side=3, line=-1.6, adj=0.03, cex=1.4)
points(AF_nmds_all$points, col=6, bg=c(2:5)[eDNA2022$Zone], 
       pch=c(21:25)[eDNA2022$Zone], cex=1.8)
ordispider(AF_nmds_all, groups=eDNA2022$Zone, col=c(2:5), lwd=1)
shadowtext(tapply(AF_nmds_all$points[,1], eDNA2022$Zone, mean),
           tapply(AF_nmds_all$points[,2], eDNA2022$Zone, mean),
           labels=levels(eDNA2022$Zone), cex=1.2, col=c(2:5), bg=6, r=0.2)
legend("bottomright", ncol=1, bty="n", legend=levels(eDNA2022$Zone), col=c(2:5), 
       pt.bg=c(2:5), lty=1, lwd=2, pch=c(21:24), pt.cex=1.8, cex=1.2)
Figure 8: Ordination plots made using a combination of functions from the `vegan` package and base-R, showing (a) 75% CI ellipses around Zone groups; (b) convex hulls around Site groups; (c) ‘spiders’ around Zone groups. The `shadowtext()` function from the `teachingDemos::` package is used for clearer labels.

Figure 8: Ordination plots made using a combination of functions from the vegan package and base-R, showing (a) 75% CI ellipses around Zone groups; (b) convex hulls around Site groups; (c) ‘spiders’ around Zone groups. The shadowtext() function from the teachingDemos:: package is used for clearer labels.

 

Optional Extras

NMDS stress plots

par(mfrow=c(1,1), mar=c(3,3,0.5,0.5), mgp=c(1.5,0.2,0), tcl=0.2, font.lab=2)
stressplot(AF_nmds_all)
Figure 7: Stress plot showing the goodness-of-fit between actual dsiimilarity for the Ashfield Flats 2022 eDNA data and dissimilarities predicted by NMDS ordination distances.

Figure 7: Stress plot showing the goodness-of-fit between actual dsiimilarity for the Ashfield Flats 2022 eDNA data and dissimilarities predicted by NMDS ordination distances.

Analysis with just the aquatic species

These are: Gambusia holbrooki, Arenigobius bifrenatus, Mugil cephalus, Acanthopagrus butcheri, Limnodynastes dorsalis, Pseudophryne guentheri, Blackfordia polytentaculata, Obelia bidentata, Phyllorhiza punctata, Aurelia sp.

fish <- allSpecies[,c(1:6,17:20)]
head(fish)
##   MosqFish Goby Mullet Bream BnjoFrog GToadlet HydroidB HydroidO BwnJelly MoonJely
## 1        1    0      0     0        1        0        0        0        0        0
## 2        1    0      0     0        1        1        0        0        0        0
## 3        1    0      0     0        0        1        0        0        0        0
## 4        1    0      0     0        1        1        0        0        0        0
## 5        1    0      0     0        1        0        0        0        0        0
## 6        1    0      0     0        1        0        0        0        0        0

for interest look at the dissimilarity matrix

AF_diss_aquat <- vegdist(fish, 
                           distance = "bray")
txt0 <- capture.output(print(AF_diss_aquat,digits=3));txt0[1:10];rm(txt0)
##         1      2      3      4      5      6      7      8      9     10     11     12     13     14     15     16     17     18     19     20     21     22     23
## 2  0.2000
## 3  0.5000 0.2000
## 4  0.2000 0.0000 0.2000
## 5  0.0000 0.2000 0.5000 0.2000
## 6  0.0000 0.2000 0.5000 0.2000 0.0000
## 7  0.0000 0.2000 0.5000 0.2000 0.0000 0.0000
## 8  0.0000 0.2000 0.5000 0.2000 0.0000 0.0000 0.0000
## 9  0.5000 0.2000 0.0000 0.2000 0.5000 0.5000 0.5000 0.5000
## 10 0.3333 0.5000 0.3333 0.5000 0.3333 0.3333 0.3333 0.3333 0.3333
## 11 1.0000 0.5000 0.3333 0.5000 1.0000 1.0000 1.0000 1.0000 0.3333 1.0000
AF_nmds_aquat <- metaMDS(fish, trymax = 500, 
                           distance = "jaccard")
AF_nmds_aquat
##
## Call:
## metaMDS(comm = fish, distance = "jaccard", trymax = 500)
##
## global Multidimensional Scaling using monoMDS
##
## Data:     fish
## Distance: jaccard
##
## Dimensions: 2
## Stress:     0.08664754
## Stress type 1, weak ties
## Best solution was repeated 1 time in 20 tries
## The best solution was from try 1 (random start)
## Scaling: centring, PC rotation, halfchange scaling
## Species: expanded scores based on 'fish'
par(mfrow=c(1,1), mar=c(3,3,2,0.5), mgp=c(1.5,0.2,0), tcl=0.2, font.lab=2)
plot(AF_nmds_aquat, type="p",display = "sites", cex = 1.2)
text(AF_nmds_aquat, display = "species", col = "royalblue", cex = 0.8)
Figure 8: Basic NMDS plot showing observations as symbols and species as text. Presence-absence data from eDNA sampling and analysis at Ashfield Flats.

Figure 8: Basic NMDS plot showing observations as symbols and species as text. Presence-absence data from eDNA sampling and analysis at Ashfield Flats.

alternative to vegan plots

palette(c("black", plasma(10))) # a viridis palette
par(mfrow=c(1,1), mar=c(3,3,0.5,5), mgp=c(1.5,0.2,0), tcl=0.2, font.lab=2, xpd=T)
plot(AF_nmds_aquat$points, pch=c(rep(21:25, 2),21)[eDNA2022$Site], 
     xlim = c(-1.2,1.8), 
     bg = seq(1,11)[eDNA2022$Site], 
     cex = 1.2, col.main = "steelblue")
text(AF_nmds_aquat, display = "species", col = "grey33", font=3, cex = 0.8)
legend(2.2,1.2, legend=seq(1,11), bty="n", title="Site", cex=1, pt.cex=1.2, 
       pch=c(rep(21:25, 2),21), pt.bg=1:11)
Figure 9: Two-dimensional non-metric multidimensional scaling (NMDS) plot showing observations as symbols and species as text located in MDS2 vs. MDS1 space. Presence-absence data for aquatic species only from eDNA sampling and analysis at Ashfield Flats.

Figure 9: Two-dimensional non-metric multidimensional scaling (NMDS) plot showing observations as symbols and species as text located in MDS2 vs. MDS1 space. Presence-absence data for aquatic species only from eDNA sampling and analysis at Ashfield Flats.

Detrended correspondence analysis - all species

AF_dca_all <- decorana(allSpecies)
AF_dca_all
##
## Call:
## decorana(veg = allSpecies)
##
## Detrended correspondence analysis with 26 segments.
## Rescaling of axes with 4 iterations.
## Total inertia (scaled Chi-square): 2.1786
##
##                        DCA1   DCA2   DCA3    DCA4
## Eigenvalues          0.5537 0.3602 0.2545 0.17011
## Additive Eigenvalues 0.5537 0.3596 0.2461 0.16058
## Decorana values      0.5632 0.3154 0.1915 0.07493
## Axis lengths         3.2842 2.6926 2.1577 1.75539
par(mfrow=c(1,1), mar=c(3,3,0.5,0.5), mgp=c(1.5,0.2,0), tcl=0.2, font.lab=2)
plot(AF_dca_all, col=c(1,4))
Figure 10: Two-dimensional detrended correspondence analysis (DCA) plot showing observations as symbols and species as text. Presence-absence data from water sampling and eDNA analysis at Ashfield Flats.

Figure 10: Two-dimensional detrended correspondence analysis (DCA) plot showing observations as symbols and species as text. Presence-absence data from water sampling and eDNA analysis at Ashfield Flats.

For comparison if wanted: nmMDS again with Bray-Curtis distances

AF_nmds_all_B <- metaMDS(allSpecies) # uses Bray by default
AF_nmds_all_B
##
## Call:
## metaMDS(comm = allSpecies)
##
## global Multidimensional Scaling using monoMDS
##
## Data:     allSpecies
## Distance: bray
##
## Dimensions: 2
## Stress:     0.1554552
## Stress type 1, weak ties
## Best solution was repeated 2 times in 20 tries
## The best solution was from try 15 (random start)
## Scaling: centring, PC rotation, halfchange scaling
## Species: expanded scores based on 'allSpecies'

The results should be identical to nmMDS with Jaccard, since the community matrix is already converted to presence-absence (binary ones and zeros).

 

References and R Packages

Dunnington, Dewey (2024). prettymapr: Scale Bar, North Arrow, and Pretty Margins in R. R package version 0.2.5. CRAN.R-project.org/package=prettymapr.

Giraud T (2025). maptiles: Download and Display Map Tiles. R package version 0.10.0, CRAN.R-project.org/package=maptiles.

Garnier S, Ross N, Rudis R, Camargo AP, Sciaini M, Scherer C (2024). viridis(Lite) – Colorblind-Friendly Color Maps for R. viridis package version 0.6.5. doi:0.5281/zenodo.4679423, sjmgarnier.github.io/viridis/

Gohel D, Skintzos P (2026). flextable: Functions for Tabular Reporting. R package version 0.9.11, CRAN.R-project.org/package=flextable.

Oksanen J, Simpson G, Blanchet F, Kindt R, Legendre P, Minchin P, O'Hara R, Solymos P, Stevens M, Szoecs E, Wagner H, Bedward M, Bolker B, Borcard D, Carvalho G, De Caceres M, Durand S, Evangelista H, Hannigan G, Hill M, Lahti L, Martino C, Ouellette M, Ribeiro Cunha E, Smith T, Stier A, Ter Braak C, Weedon J (2026). vegan: Community Ecology Package. R package version 2.7-5, CRAN.R-project.org/package=vegan.

Pebesma, E., 2018. Simple Features for R: Standardized Support for Spatial VectorData. The R Journal 10(1), 439-446, doi:10.32614/RJ-2018-009. (sf package)

Tidy Ecology, 2026. Pairwise PERMANOVA in R: which groups differ. R & QGIS tutorials for ecologists, https://tidyecology.com/posts/pairwise-permanova/

Wickham H (2025). stringr: Simple, Consistent Wrappers for Common String Operations. R package version 1.5.2, CRAN.R-project.org/package=stringr.


CC-BY-SA • All content by Ratey-AtUWA. My employer does not necessarily know about or endorse the content of this website.
Created with rmarkdown in RStudio. Currently using the free yeti theme from Bootswatch.