Community Ecology Methods in R

Some examples & code to practice with

GK Smith & ER Moran, October 2013

Part 1. Community data & Diversity measures

library(vegan)
## Warning: package 'vegan' was built under R version 2.15.3
## Loading required package: permute
## This is vegan 2.0-7
library(Hotelling)
## Loading required package: corpcor

To begin, let's open up one of the built-in data sets, called dune:

data(dune)

We can examine the data

head(dune)
##    Belper Empnig Junbuf Junart Airpra Elepal Rumace Viclat Brarut Ranfla
## 2       3      0      0      0      0      0      0      0      0      0
## 13      0      0      3      0      0      0      0      0      0      2
## 4       2      0      0      0      0      0      0      0      2      0
## 16      0      0      0      3      0      8      0      0      4      2
## 6       0      0      0      0      0      0      6      0      6      0
## 1       0      0      0      0      0      0      0      0      0      0
##    Cirarv Hyprad Leoaut Potpal Poapra Calcus Tripra Trirep Antodo Salrep
## 2       0      0      5      0      4      0      0      5      0      0
## 13      0      0      2      0      2      0      0      2      0      0
## 4       2      0      2      0      4      0      0      1      0      0
## 16      0      0      0      0      0      3      0      0      0      0
## 6       0      0      3      0      3      0      5      5      3      0
## 1       0      0      0      0      4      0      0      0      0      0
##    Achmil Poatri Chealb Elyrep Sagpro Plalan Agrsto Lolper Alogen Brohor
## 2       3      7      0      4      0      0      0      5      2      4
## 13      0      9      1      0      2      0      5      0      5      0
## 4       0      5      0      4      5      0      8      5      2      3
## 16      0      2      0      0      0      0      7      0      4      0
## 6       2      4      0      0      0      5      0      6      0      0
## 1       1      2      0      4      0      0      0      7      0      0
names(dune)
##  [1] "Belper" "Empnig" "Junbuf" "Junart" "Airpra" "Elepal" "Rumace"
##  [8] "Viclat" "Brarut" "Ranfla" "Cirarv" "Hyprad" "Leoaut" "Potpal"
## [15] "Poapra" "Calcus" "Tripra" "Trirep" "Antodo" "Salrep" "Achmil"
## [22] "Poatri" "Chealb" "Elyrep" "Sagpro" "Plalan" "Agrsto" "Lolper"
## [29] "Alogen" "Brohor"

So we have a data set that comprises counts of species abundance measured in a series of sampling sites.

Now let's look at number of species and the number of sampling sites in the dataset:

ncol(dune)  # for the number of species
## [1] 30
nrow(dune)  # for the number of sites
## [1] 20

Alright, on to the fun stuff: diversity! We begin by calculating the Shannon and Simpson index values for each site using the diversity function.

shann <- diversity(dune)  # for the Shannon index
shann
##     2    13     4    16     6     1     8     5    17    15    10    11 
## 2.253 2.100 2.427 1.960 2.346 1.440 2.435 2.544 1.876 1.979 2.399 2.106 
##     9    18     3    20    14    19    12     7 
## 2.494 2.079 2.194 2.048 1.864 2.134 2.114 2.472
simp <- diversity(dune, "simpson")  # Simpson index
simp
##      2     13      4     16      6      1      8      5     17     15 
## 0.8900 0.8522 0.9007 0.8430 0.9002 0.7346 0.9087 0.9140 0.8356 0.8507 
##     10     11      9     18      3     20     14     19     12      7 
## 0.9032 0.8672 0.9116 0.8615 0.8788 0.8678 0.8333 0.8741 0.8686 0.9075
par(mfrow = c(1, 2))  # to generate panels with 1 row of 2 graphs
hist(shann)
hist(simp)

plot of chunk unnamed-chunk-5

Next we can calcuate pair-wise distance measures between sites based on their species composition:

par(mfrow = c(1, 2))
bray = vegdist(dune, "bray")
gower = vegdist(dune, "gower")
hist(bray)
hist(gower)

plot of chunk unnamed-chunk-6

To try some rarefaction, we use the rarefy and rarecurve functions.

sp.abund <- rowSums(dune)  #gives the number of individuals found in each plot
raremax <- min(rowSums(dune))  #rarefaction uses the smallest number of observations (individuals here) per sample to extrapolate the expected number if all other samples only had that number of observations
raremax
## [1] 15
Srare <- rarefy(dune, raremax)
par(mfrow = c(1, 2))
plot(sp.abund, Srare, xlab = "Observed No. of Species", ylab = "Rarefied No. of Species")
rarecurve(dune, col = "blue")

plot of chunk unnamed-chunk-7

# notice that rarefied numbers are generally lower than observed, as
# expected notice that they mostly asymptote, indicating thorough sampling