Lab 26: Measuring Species Diversity in R — Part I

BI 255 · Bethune-Cookman University · Fall 2026

Author

Dr. Rosie Stanbrook-Buyer

Published

October 28, 2026


Quantifying Biodiversity

Biodiversity is more than just a count of species. A forest with 10 species, nine of which are rare and one of which makes up 95% of all individuals, is less diverse in a meaningful sense than a forest where 10 species are evenly distributed. Diversity indices capture both richness (how many species) and evenness (how equally abundant they are).

NoteLearning Objectives

By the end of this lab you will be able to:

  1. Distinguish between species richness, Shannon diversity, and Simpson’s index
  2. Install and use the vegan package for ecological data analysis
  3. Compute diversity indices manually and with vegan::diversity()
  4. Understand what evenness means and calculate Pielou’s J
  5. Construct a species abundance distribution plot
  6. Compare diversity across sites and interpret differences biologically

Part 1: Species Richness — The Simplest Measure

Species richness (S) is simply the number of species present in a sample. It is the most intuitive measure but ignores abundance distribution entirely.

# Three Caribbean reef fish survey sites
# Columns = species, rows = sites
# Values = number of individuals observed

site_A <- c(Parrotfish = 45, Snapper = 42, Grouper = 40, Angelfish = 38,
            Tang = 35,  Wrasse = 30, Damselfish = 25, Triggerfish = 20)

site_B <- c(Parrotfish = 180, Snapper = 8, Grouper = 4, Angelfish = 3,
            Tang = 2, Wrasse = 2, Damselfish = 1, Triggerfish = 0)

site_C <- c(Parrotfish = 60, Snapper = 55, Grouper = 0, Angelfish = 0,
            Tang = 48, Wrasse = 0, Damselfish = 0, Triggerfish = 0)

cat("Species richness:\n")
Species richness:
cat("  Site A:", sum(site_A > 0), "species\n")
  Site A: 8 species
cat("  Site B:", sum(site_B > 0), "species\n")
  Site B: 7 species
cat("  Site C:", sum(site_C > 0), "species\n")
  Site C: 3 species
WarningThe Problem with Richness Alone

Sites A and B both have 8 species — but intuitively, Site A is far more diverse because individuals are spread evenly across species. Site B is dominated by a single species (Parrotfish = 180 of the total).

Species richness misses this. Diversity indices that incorporate both richness and evenness give a more complete picture.


Part 2: Shannon Diversity Index

The Shannon index (H’) was originally derived from information theory. It measures the uncertainty in predicting the species identity of a randomly chosen individual. High H’ means many species and/or even abundance distribution.

H’ = −Σ(pᵢ × ln(pᵢ))

where pᵢ is the proportion of all individuals belonging to species i.

# Manual calculation of Shannon's H'
shannon_manual <- function(counts) {
  counts <- counts[counts > 0]            # Remove absent species
  props  <- counts / sum(counts)          # Convert to proportions
  H      <- -sum(props * log(props))     # Shannon formula (natural log)
  return(H)
}

cat("Shannon H' (manual):\n")
Shannon H' (manual):
cat("  Site A:", round(shannon_manual(site_A), 4), "\n")
  Site A: 2.0498 
cat("  Site B:", round(shannon_manual(site_B), 4), "\n")
  Site B: 0.4834 
cat("  Site C:", round(shannon_manual(site_C), 4), "\n")
  Site C: 1.0945 
# Using the vegan package
# install.packages("vegan")
library(vegan)

community_matrix <- rbind(Site_A = site_A,
                           Site_B = site_B,
                           Site_C = site_C)

# Shannon diversity (vegan uses natural log by default)
diversity(community_matrix, index = "shannon")
   Site_A    Site_B    Site_C 
2.0497710 0.4834105 1.0944763 
NoteInterpreting Shannon’s H’
H’ value Interpretation (rough guide)
0 Only one species present
1–2 Low diversity
2–3 Moderate diversity
> 3 High diversity

H’ has no absolute maximum — it depends on species richness. A community with S species and perfectly equal abundances achieves H’_max = ln(S). Always interpret H’ relative to the maximum possible for that richness.


Part 3: Simpson’s Diversity Index

The Simpson index measures the probability that two individuals drawn randomly from the same sample belong to different species. It ranges from 0 (no diversity) to 1 (maximum diversity).

D = 1 − Σ(pᵢ²)

# Simpson's index (1 - D in vegan notation)
diversity(community_matrix, index = "simpson")
   Site_A    Site_B    Site_C 
0.8679934 0.1875500 0.6639316 
# Manual calculation for comparison
simpson_manual <- function(counts) {
  counts <- counts[counts > 0]
  props  <- counts / sum(counts)
  D      <- 1 - sum(props^2)
  return(D)
}

cat("Simpson 1-D (manual):\n")
Simpson 1-D (manual):
cat("  Site A:", round(simpson_manual(site_A), 4), "\n")
  Site A: 0.868 
cat("  Site B:", round(simpson_manual(site_B), 4), "\n")
  Site B: 0.1875 
cat("  Site C:", round(simpson_manual(site_C), 4), "\n")
  Site C: 0.6639 
TipShannon vs Simpson — Which to Use?

Shannon H’ is more sensitive to rare species — adding even one rare species increases H’ more than it increases Simpson’s D. Use Shannon when rare species matter (e.g., conservation planning).

Simpson D (1−D) is dominated by abundant species and changes little when very rare species are added or removed. Use when you want a measure robust to sampling rare species.

Both are valid. Many ecologists report both, along with species richness.


Part 4: Evenness — Pielou’s J

Pielou’s J rescales Shannon diversity relative to the theoretical maximum for that richness level:

J = H’ / H’_max = H’ / ln(S)

# Calculate Pielou's J for all three sites
S <- specnumber(community_matrix)   # species richness per site
H <- diversity(community_matrix, index = "shannon")

J <- H / log(S)   # Pielou's evenness

cat("Species richness (S):\n"); print(S)
Species richness (S):
Site_A Site_B Site_C 
     8      7      3 
cat("\nShannon H':\n"); print(round(H, 3))

Shannon H':
Site_A Site_B Site_C 
 2.050  0.483  1.094 
cat("\nPielou's J (evenness, 0–1):\n"); print(round(J, 3))

Pielou's J (evenness, 0–1):
Site_A Site_B Site_C 
 0.986  0.248  0.996 
NoteInterpreting Pielou’s J

J ranges from 0 (all individuals in one species) to 1 (perfectly equal abundances across all species).

Site A: J ≈ 1.0 — nearly perfect evenness. Each species contributes similarly.

Site B: J ≈ 0.3 — low evenness despite equal richness. One species dominates.

Site C: J ≈ 1.0 — only 3 species but they are evenly abundant.

This illustrates why diversity = richness × evenness: Site A has both, Site C has evenness but not richness, Site B has richness but not evenness.


Part 5: Species Abundance Distribution (SAD)

A species abundance distribution (rank-abundance curve) plots species in decreasing order of abundance. The shape of this curve reveals the overall evenness of a community.

library(ggplot2)

# Prepare rank-abundance data
rad_data <- function(site_vec, site_name) {
  s  <- site_vec[site_vec > 0]
  s  <- sort(s, decreasing = TRUE)
  data.frame(
    Rank      = seq_along(s),
    Abundance = s,
    Species   = names(s),
    Site      = site_name
  )
}

rad_all <- rbind(rad_data(site_A, "Site A"),
                 rad_data(site_B, "Site B"),
                 rad_data(site_C, "Site C"))

ggplot(rad_all, aes(x = Rank, y = Abundance, colour = Site, group = Site)) +
  geom_line(linewidth = 1.1) +
  geom_point(size = 2.5) +
  scale_colour_manual(values = c("Site A" = "#2C5F8A",
                                 "Site B" = "#C8102E",
                                 "Site C" = "#5B8DB8")) +
  labs(title    = "Rank-Abundance Curves for Three Caribbean Reef Fish Communities",
       subtitle  = "Flat curves = high evenness; steep curves = low evenness",
       x        = "Species Rank (most to least abundant)",
       y        = "Number of Individuals") +
  theme_classic(base_size = 13)

NoteReading a Rank-Abundance Curve
  • Flat, wide curve (Site A): many species, similar abundances — high richness and high evenness
  • Steep, narrow curve (Site B): one dominant species drops off sharply — low evenness
  • Short, flat curve (Site C): few species but equal abundances — low richness, high evenness

The length of the curve on the x-axis shows richness. The steepness shows how unevenly distributed the individuals are.


3-Minute Knowledge Check

Close your notes. Answer these on your own — you have 3 minutes. We’ll go through the answers together after.

CautionKnowledge Check Questions

1. Two forest plots each have 10 tree species. Plot 1 has 10 individuals of each species (100 total). Plot 2 has 91 individuals of one species and 1 of each of the other 9 species (100 total). Which plot has higher Shannon H’ and why?

2. You calculate Simpson’s 1−D for a grassland community and get 0.08. What does this tell you?

3. A site has H’ = 2.1 and S = 15 species. What is Pielou’s J, and is the community considered even?

4. On a rank-abundance curve, what does a long, nearly flat line indicate compared to a short, steep line?

5. True or False: It is possible for two communities to have the same species richness but very different Shannon diversity values.

1. Plot 1 has higher Shannon H’. Shannon diversity is maximised when all species are equally abundant — Plot 1 has perfect evenness (J = 1.0). In Plot 2, the dominant species contributes −91/100 × ln(91/100) ≈ 0.09 to H’, while the nine rare species each contribute only −1/100 × ln(1/100) ≈ 0.046 — resulting in a much lower total H’. Intuitively, you could almost predict the species identity of a random individual in Plot 2 (it’s probably the dominant one), so uncertainty (= diversity) is low.

2. Simpson’s 1−D = 0.08 is very low — close to 0 means very low diversity. The probability that two randomly drawn individuals come from different species is only 8%. This suggests the community is strongly dominated by one or a few species. This might indicate environmental stress, disturbance, or competitive dominance.

3. H’_max = ln(15) = 2.708. Pielou’s J = 2.1 / 2.708 = 0.77. A value of 0.77 is moderately high evenness — the community is reasonably well-distributed across species, reaching about 77% of the maximum possible diversity for 15 species. Not perfectly even but substantially more even than a dominated community.

4. A long, nearly flat rank-abundance curve indicates high species richness with high evenness — many species are present and their abundances are similar. A short, steep curve indicates low richness and/or low evenness — few species and/or a rapid decline from a dominant species to rare ones.

5. True — Shannon diversity depends on both richness AND evenness. Two communities with S = 10 species can have very different H’ values if their abundance distributions differ. The community with even abundances will have H’ near ln(10) ≈ 2.3; the community dominated by one species will have H’ close to 0.


Lab 26 Checklist

Before you leave, make sure you can:

TipBonus Challenge

R’s vegan package includes the BCI dataset — a classic census of tree species on 50 plots in Barro Colorado Island (Panama). Load it with data(BCI). Calculate Shannon H’, Simpson 1−D, and Pielou’s J for each of the 50 plots. Plot the distribution of Shannon H’ across plots as a histogram. Do the plots vary substantially in diversity? What ecological factors might explain this variation?


Before Next Class (Monday, Week 12)

  • Monday (Lab 27): Species Diversity Part II — rarefaction curves, diversity comparisons, and NMDS ordination
  • Review vegan package documentation: ?diversity, ?rarecurve, ?metaMDS