4  Diversity Indices

Week 4: Measuring abundance, richness and diversity

How many individuals are there? How many species? How evenly are individuals spread among those species? In this workshop you will calculate the most common measures of abundance, species richness and diversity from survey data. These skills are essential for field-based projects and for jobs in ecological consulting and monitoring. Workshop materials are available in the GitHub repository ECS200.

TipLearning outcomes

By the end of this workshop you should be able to:

  1. Calculate absolute abundance, relative abundance and density.
  2. Calculate species richness, and explain when rarefied richness is needed.
  3. Calculate and interpret the Shannon and Simpson diversity indices.
  4. Choose a suitable metric for a survey design and justify your choice.

Background

Biodiversity has two main components: richness (how many species) and evenness (how equally individuals are spread among the species). Different metrics capture different combinations of these.

Metric What it measures Watch out for
Absolute abundance (\(N_i\)) Number of individuals of a species Depends on sampling effort. Only compare surveys with the same effort
Relative abundance (\(p_i\)) Proportion of all individuals that belong to a species Hides differences in total abundance
Density (\(d_i\)) Individuals per unit area or effort Needs an accurate measure of area or effort
Species richness (\(S\)) Number of species Increases with sampling effort and is sensitive to rare species
Rarefied richness Richness standardised to the same number of individuals Throws away information when samples differ a lot in size
Shannon index (\(H'\)) Richness and evenness together Hard to interpret in real-world terms. Moderately sensitive to rare species
Simpson index (\(D\)) Mostly evenness and dominance Not very sensitive to rare species

Other measures, such as alpha, beta and gamma diversity (diversity within a site, between sites and across a region) and taxonomic diversity, can be calculated with the vegan package.

Use the app to see how richness and evenness change the Shannon and Simpson indices.

App: richness, evenness and diversity

Before we start

Code
# Week 4 workshop - Diversity indices
# Written by Nicholas Wu, 1234578, 18/03/2027

# install.packages("vegan")  # run once in the console if needed

library(tidyverse)  # data wrangling and plotting
library(vegan)      # rarefied richness (and many other community ecology tools)

# setwd("C:/Users/YOUR-NAME/Documents/ECS200")

We start with a small example survey: five samples at each of three sites, with the number of individuals of five species in each sample.

Code
survey_data <- data.frame(
  site      = c("A", "A", "A", "A", "A", "B", "B", "B", "b", "B", "C", "C", "C", "C", "C"),
  species_1 = c(57, 73, 20, 75, 83, 41, 6, 61, 45, 2, 0, 93, 0, 0, 86),
  species_2 = c(31, 12, 4, 41, 4, 49, 2, 7, 7, 9, 54, 64, 35, 38, 53),
  species_3 = c(0, 38, 3, 0, 39, 0, 29, 24, 15, 23, 10, 0, 1, 5, 2),
  species_4 = c(11, 28, 19, 0, 1, 34, 7, 0, 1, 0, 1, 0, 12, 45, 5),
  species_5 = c(0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 3, 1, 0, 1, 2)
)

# Quality check: one site was entered as "b" instead of "B"
survey_clean <- survey_data %>%
  dplyr::mutate(site = dplyr::if_else(site == "b", "B", site),
                site = factor(site))

# Long format (Week 2): one row per sample and species. This makes
# group-by-group calculations much easier.
survey_long <- survey_clean %>%
  dplyr::mutate(sample = dplyr::row_number()) %>%     # give each sample (row) a number
  tidyr::pivot_longer(cols      = species_1:species_5,
                      names_to  = "species",
                      values_to = "abundance")

head(survey_long)
# A tibble: 6 × 4
  site  sample species   abundance
  <fct>  <int> <chr>         <dbl>
1 A          1 species_1        57
2 A          1 species_2        31
3 A          1 species_3         0
4 A          1 species_4        11
5 A          1 species_5         0
6 A          2 species_1        73

Part 1: Abundance

Exercise 1 (10 min)

🧪 Calculate abundance metrics for this survey

Code
exercise_data <- data.frame(
  site      = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
  plot      = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
  species_1 = c(12, 7, 15, 4, 9, 6, 20, 18, 25),
  species_2 = c(5, 2, 1, 10, 7, 4, 3, 1, 0),
  species_3 = c(0, 3, 2, 6, 4, 5, 0, 1, 2),
  species_4 = c(8, 6, 3, 1, 4, 2, 10, 7, 5)
)
  1. What is the total (absolute) abundance of species 1 across all sites?
  2. What proportion of all individuals at site B belong to species 2?
  3. What is the mean density (individuals per plot) of species 3 at each site?
Show answer
Code
exercise_long <- exercise_data %>%
  tidyr::pivot_longer(cols = species_1:species_4, names_to = "species", values_to = "abundance")

# 1. Total abundance of species 1
exercise_long %>%
  dplyr::filter(species == "species_1") %>%
  dplyr::summarise(total = sum(abundance))         # 116
# A tibble: 1 × 1
  total
  <dbl>
1   116
Code
# 2. Relative abundance of each species at site B
exercise_long %>%
  dplyr::filter(site == "B") %>%
  dplyr::group_by(species) %>%
  dplyr::summarise(total = sum(abundance)) %>%
  dplyr::mutate(rel_abundance = total / sum(total)) # species_2 = 21 / 62 = 0.34
# A tibble: 4 × 3
  species   total rel_abundance
  <chr>     <dbl>         <dbl>
1 species_1    19         0.306
2 species_2    21         0.339
3 species_3    15         0.242
4 species_4     7         0.113
Code
# 3. Mean density of species 3 per plot at each site
exercise_long %>%
  dplyr::filter(species == "species_3") %>%
  dplyr::group_by(site) %>%
  dplyr::summarise(density = mean(abundance))      # A = 1.67, B = 5, C = 1
# A tibble: 3 × 2
  site  density
  <chr>   <dbl>
1 A        1.67
2 B        5   
3 C        1   

Part 2: Species richness

Part 3: Diversity indices

Exercise 2 (10 min)

🧪 Calculate richness and diversity for exercise_data

  1. What is the species richness at each site?
  2. What is the Shannon diversity of site A?
  3. What is the inverse Simpson index of site B?
Show answer
Code
exercise_rel <- exercise_long %>%
  dplyr::group_by(site, species) %>%
  dplyr::summarise(total = sum(abundance), .groups = "drop") %>%
  dplyr::group_by(site) %>%
  dplyr::mutate(p = total / sum(total)) %>%
  dplyr::filter(p > 0)

exercise_rel %>%
  dplyr::summarise(richness    = dplyr::n_distinct(species),
                   shannon     = -sum(p * log(p)),
                   inv_simpson = 1 / sum(p^2))
# A tibble: 3 × 4
  site  richness shannon inv_simpson
  <chr>    <int>   <dbl>       <dbl>
1 A            4   1.15         2.67
2 B            4   1.32         3.57
3 C            4   0.849        1.89
Code
# 1. A = 4, B = 4, C = 4 species
# 2. Shannon (site A) = 1.15
# 3. Inverse Simpson (site B) = 3.57

Assessment

Task to complete before the end of the workshop.

week04_assessment.R invert_exp_survey.csv

Does drought change soil invertebrate communities? This real dataset comes from a field experiment in which rain-out shelters created an experimental drought, next to control plots, in 5 blocks. Invertebrates were caught in pitfall traps (3 traps per plot, sampled in several months) (Farkas et al., 2025). Invertebrates were identified to order, except Crustacea (subphylum), Diplopoda and Chilopoda (class) and Annelida (phylum). We will call these groups “orders”.

  1. Import the data and run a quality check (structure, missing values, duplicates).
  2. Convert the data to long format with one row per sample and order. Leave out the unidentified column. Why should unidentified individuals not be counted as a separate group when calculating richness?
  3. Calculate the relative abundance of each order in the control and drought treatments, and plot it. Which order dominates both treatments?
  4. Calculate the richness (number of orders with at least one individual) in each treatment.
  5. Calculate the Shannon diversity index for each treatment and plot it.
  6. Interpretation: in two or three sentences, describe how drought affected the invertebrate community, using your answers to Q3–Q5.

The relative abundance figure might look like this:

Submit one R script (.R) before the end of the class. Write your answers to written questions as # comments next to the code that produced them.

Score What it looks like
0 No submission or no attempt.
0.25 Submitted, but a very poor attempt (e.g. incomplete, or lots of gaps).
0.5 Most questions answered, or all answered but most annotations missing.
0.75 All questions answered, but some annotations missing or some answers incorrect.
1 Everything completed, with annotations on all code so the marker can follow it.

Tip: an annotation is a short # comment that says what the line does and why. For example: # remove the one plant with a stem dry mass of 7.4 g (data entry error).

For the adventurous

Calculate the Shannon index for each treatment × block combination and plot the values for each treatment. This shows the variation among experimental blocks, which is what your report should show, rather than a single value per treatment.

Additional resources