---
title: "Janzen-Connell and tree diversity"
description: "The Janzen-Connell hypothesis in R: how specialised natural enemies near parent trees give rare species an edge and turn competitive exclusion into coexistence."
date: "2026-08-01 12:00"
categories: [ecology tutorial, R, community ecology, biodiversity, coexistence]
image: thumbnail.png
image-alt: "Species abundances under two rules: without density dependence one species dominates, with it all species coexist evenly."
---
```{r setup}
#| include: false
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
dev = "png", dpi = 120, fig.path = "figures/",
fig.width = 7, fig.height = 4.4)
library(ggplot2)
theme_te <- theme_minimal(base_size = 12) +
theme(panel.background = element_rect(fill = "white", colour = NA),
plot.background = element_rect(fill = "white", colour = NA),
panel.grid.minor = element_blank(),
plot.title = element_text(face = "bold", size = 12))
theme_set(theme_te)
col_no <- "#b5651d"; col_cndd <- "#3a6b52"; col_ref <- "#555555"
simcomm <- function(cndd, S = 8, T = 400, seed = 415) {
set.seed(seed); f <- rep(1/S, S); fit <- runif(S, 0.9, 1.1)
for (t in 1:T) { w <- fit * f * exp(-cndd * f); f <- w / sum(w) }
f
}
shannon <- function(f) { f <- f[f > 0]; -sum(f * log(f)) }
f_no <- simcomm(0); f_cndd <- simcomm(6)
S_no <- sum(f_no > 0.005); S_cndd <- sum(f_cndd > 0.005)
H_no <- shannon(f_no); H_cndd <- shannon(f_cndd); Hmax <- log(8)
```
A hectare of tropical forest can hold several hundred tree species, and this is a genuine puzzle. Simple competition theory says the best competitor should win, and a species that is even slightly superior should, given time, take over. So why do so many species persist side by side, none of them running away with the forest? Janzen 1970 The American Naturalist 104(940):501-528 and Connell 1971 gave the same answer independently: the enemies of a tree do the work that competition cannot.
## Enemies that punish being common
The Janzen-Connell hypothesis is about specialised natural enemies, the seed predators, insect herbivores and soil pathogens that attack one tree species and not others. These enemies build up wherever their host is abundant, in the soil beneath a parent tree and in patches where the species is locally common. A seed or seedling landing among many of its own kind is landing in a reservoir of its own specialised enemies, and it dies at a higher rate. The result is that a species suffers most exactly where it is most common, which caps every species before it can dominate and leaves room for the rest (Connell JH 1971, in den Boer and Gradwell, Dynamics of Populations: 298-312).
Model a whole community this way. Give each species a per-capita recruitment that falls as its own local frequency rises, the community-level footprint of its specialised enemies, and let the community iterate.
```{r community}
simcomm <- function(cndd, S = 8, T = 400, seed = 415) {
set.seed(seed); f <- rep(1/S, S); fit <- runif(S, 0.9, 1.1) # small fitness differences
for (t in 1:T) {
w <- fit * f * exp(-cndd * f) # enemies cut recruitment where a species is common
f <- w / sum(w)
}
f
}
round(rbind(no_enemies = simcomm(0), with_enemies = simcomm(6)), 3)
```
## Diversity from being punished
Without the density-dependent penalty, the community collapses to a single species: the one with the highest intrinsic fitness slowly excludes every other, and diversity falls to nothing. Turn the enemies on and all `r sprintf("%.0f", 8)` species persist, at nearly even abundances.
```{r diversity}
shannon <- function(f) { f <- f[f > 0]; -sum(f * log(f)) }
c(species_no_enemies = sum(simcomm(0) > 0.005), shannon_no_enemies = shannon(simcomm(0)),
species_with_enemies = sum(simcomm(6) > 0.005), shannon_with_enemies = shannon(simcomm(6)))
```
Without enemies, `r sprintf("%.0f", S_no)` species survives and Shannon diversity is `r sprintf("%.2f", H_no)`. With them, `r sprintf("%.0f", S_cndd)` species coexist and diversity reaches `r sprintf("%.2f", H_cndd)`, which is essentially the maximum possible for eight species, `r sprintf("%.2f", Hmax)`. The same small fitness differences that drove the forest to monoculture become harmless once every species is held back hardest when it gets ahead.
```{r fig-diversity}
#| fig-cap: "Species abundances after long competition. Without specialised enemies (ochre) the fittest species excludes the rest; with them (green) all eight coexist at even abundance."
#| fig-alt: "Two sets of bars for eight species; without enemies one bar is at one and the rest at zero, with enemies all eight bars are roughly equal."
d <- rbind(data.frame(sp = factor(1:8), f = f_no, rule = "no enemies"),
data.frame(sp = factor(1:8), f = f_cndd, rule = "with specialised enemies"))
ggplot(d, aes(sp, f, fill = rule)) +
geom_col(position = "dodge", width = 0.7) +
scale_fill_manual(values = c("no enemies" = col_no, "with specialised enemies" = col_cndd)) +
labs(x = "Species", y = "Relative abundance", fill = NULL,
title = "Specialised enemies turn exclusion into coexistence")
```
## From one hectare to a latitude gradient
Scale the argument up and it offers an explanation for one of the oldest patterns in ecology, the increase in species richness toward the equator. If specialised enemies are more effective in warm, wet, aseasonal tropics, where pests and pathogens are never knocked back by frost or drought, then density-dependent mortality should be stronger there, and stronger density dependence supports more species. Analyses of long-term forest plots have found exactly this signature (HilleRisLambers et al. 2002 Nature 417(6890):732-735). The relationship is smooth: stronger density dependence supports steadily more diversity, up to the even community the mechanism can sustain.
```{r fig-gradient}
#| fig-cap: "Community diversity against the strength of specialised-enemy density dependence. At zero it collapses to one species; as density dependence strengthens, more species coexist, toward the maximum for the pool."
#| fig-alt: "A rising curve of Shannon diversity against density-dependence strength, from near zero to a plateau at the maximum diversity for eight species."
cg <- data.frame(cndd = seq(0, 10, 0.25))
cg$H <- sapply(cg$cndd, function(c) shannon(simcomm(c)))
ggplot(cg, aes(cndd, H)) +
geom_hline(yintercept = Hmax, colour = col_ref, linetype = "dotted") +
geom_line(colour = col_cndd, linewidth = 1) +
annotate("text", x = 0.2, y = Hmax - 0.12, label = "maximum for the species pool", hjust = 0, colour = col_ref, size = 3.4) +
labs(x = "Strength of enemy density dependence", y = "Shannon diversity",
title = "Stronger density dependence, more coexisting species")
```
The mechanism behind all of it is a species doing worse where it is common, which the [next post](../conspecific-negative-density-dependence/) measures directly, and its coexistence logic is negative frequency dependence, the subject of the [third post](../rare-species-advantage-and-coexistence/). Detecting it in real forests is deceptively hard, which is the [checking post](../checking-a-cndd-analysis/).
## Related tutorials
- [Conspecific negative density dependence](../conspecific-negative-density-dependence/)
- [Rare-species advantage and coexistence](../rare-species-advantage-and-coexistence/)
- [Checking a CNDD analysis](../checking-a-cndd-analysis/)
- [Alpha diversity indices in R](../diversity-indices-in-r/)
## References
- Connell JH 1971. In: den Boer PJ, Gradwell GR (eds) Dynamics of Populations. PUDOC, Wageningen: 298-312.
- HilleRisLambers J et al. 2002. Nature 417(6890):732-735 (10.1038/nature00809).
- Janzen DH 1970. The American Naturalist 104(940):501-528 (10.1086/282687).