From taxonomical to functional and phylogenetic diversity in R

Andrea Sánchez-Tapia , Sara Mortara
8/04/2022

Back to cestes data

Remember our good practices: you do not save workspaces and every script is self-contained. In previous classes you read the CESTES database and obtained an object comm (for the community matrix).

Execute this code at the top of your new script and bring the trait dataframe too.

We are going to start at this stage: comm and traits exist and are data.frames:

head(comm)[,1:6]
  Sites sp1 sp2 sp3 sp4 sp5
1     1   0   0   0   0   0
2     2   0   1   0   0   0
3     3   0   1   0   0   0
4     4   0   0   0   0   0
5     5   0   0   0   0   0
6     6   0   0   0   0   0
head(traits)[,1:6]
   Sp Anemo Auto Entomo Annual Biennial
1 sp1     0    0      1      0        0
2 sp2     0    0      1      0        0
3 sp3     0    0      1      1        1
4 sp4     0    0      1      0        0
5 sp5     0    0      1      0        0
6 sp6     0    0      1      0        0
You can add rownames instead of having columns:
rownames(comm)[1:6]
[1] "1" "2" "3" "4" "5" "6"
rownames(comm) <- paste0("Site", comm[,1])
comm <- comm[,-1]
head(comm)[,1:6]
      sp1 sp2 sp3 sp4 sp5 sp6
Site1   0   0   0   0   0   0
Site2   0   1   0   0   0   0
Site3   0   1   0   0   0   0
Site4   0   0   0   0   0   0
Site5   0   0   0   0   0   0
Site6   0   0   0   0   0   0

Transform column traits$Sp into the rownames of the dataframe.

We want this output:

head(traits)[,1:6]
    Anemo Auto Entomo Annual Biennial Perennial
sp1     0    0      1      0        0         1
sp2     0    0      1      0        0         1
sp3     0    0      1      1        1         1
sp4     0    0      1      0        0         1
sp5     0    0      1      0        0         1
sp6     0    0      1      0        0         1

Species richness

Species richness can be calculated with the vegan package:

library(vegan)
richness <- vegan::specnumber(comm)

Taxonomic diversity

Taxonomic measures can be calculated using diversity() function:

shannon <- vegan::diversity(comm)
simpson <- vegan::diversity(comm, index = "simpson")

Functional diversity

Taxonomic diversity indices are based on the assumption that species belong to one species or the other.

This can be thought as a distance matrix between individuals, where individuals of the same species have a distance of zero between them, individuals of different species have a distance of 1 between them.

When analyzing functional traits the distance between individuals is no longer determined by their belonging to a species, but to their position in the trait space.

These traits can be continuous, but vary in different scales, or they can be categorical, and appropriate distance measures have to be used to deal with this difference. Gower distance is a common distance metric used in trait-based ecology.

library(cluster)
library(FD)
gow <- cluster::daisy(traits, metric = "gower")
gow2 <- FD::gowdis(traits)
#implementations in R vary and the literature reports extensions and modifications
identical(gow, gow2) #not the same but why?
[1] FALSE
class(gow) #different classes
[1] "dissimilarity" "dist"         
class(gow2)
[1] "dist"
plot(gow, gow2, asp = 1) #same values

Rao’s quadratic entropy calculations in R

Using package SYNCSA

#install.packages(SYNCSA)
library(SYNCSA)
tax <- rao.diversity(comm)
fun <- rao.diversity(comm, traits = traits)
#plot(fun$Simpson,fun$FunRao, pch = 19, asp = 1)
#abline(a = 0, b = 1)

Calculating FD indices with package PD

#install.packages("FD")
library(FD)
#we can use the distance matrix to calculate functional diversity indices
FuncDiv1 <- dbFD(x = gow, a = comm, messages = F)
#the returned object has Villéger's indices and Rao calculation
names(FuncDiv1)
[1] "nbsp"      "sing.sp"   "FRic"      "qual.FRic" "FEve"     
[6] "FDiv"      "FDis"      "RaoQ"     
#We can also do the calculation using the traits matrix directly
FuncDiv <- dbFD(x = traits, a = comm, messages = F)

Next: How to we summarize visually, interpret community composition and trait data?