# Ecological traps and pseudo-sinks
# https://tidyecology.com/posts/ecological-traps-and-pseudo-sinks/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-15

## ---- setup ----
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 = "#f5f4ee", colour = NA),
        plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank(),
        plot.title = element_text(face = "bold", size = 12))
theme_set(theme_te)
col_true <- "#b5651d"; col_pseudo <- "#3a6b52"; col_ref <- "#555555"

lam_true   <- function(N) rep(0.85, length(N))
lam_pseudo <- function(N) pmax(1.6 - 0.006*N, 0.01)
iso <- function(lam, N0, yr = 40) { N <- N0; for (t in 1:yr) N <- N * lam(N); N }
true_iso <- iso(lam_true, 180); pseudo_iso <- iso(lam_pseudo, 180)

## ---- sinks ----
lam_true   <- function(N) rep(0.85, length(N))          # below one at every density
lam_pseudo <- function(N) pmax(1.6 - 0.006*N, 0.01)     # good, but density-dependent
rbind(at_crowded_180 = c(true = lam_true(180), pseudo = lam_pseudo(180)),
      at_sparse_10   = c(true = lam_true(10),  pseudo = lam_pseudo(10)))

## ---- isolate ----
iso <- function(lam, N0, yr = 40) { N <- N0; for (t in 1:yr) N <- N * lam(N); N }
c(true_sink_isolated = iso(lam_true, 180), pseudo_sink_isolated = iso(lam_pseudo, 180))

## ---- fig-pseudo ----
Ng <- seq(0, 200, 1)
d <- rbind(data.frame(N = Ng, lam = lam_true(Ng),   kind = "true sink"),
           data.frame(N = Ng, lam = lam_pseudo(Ng), kind = "pseudo-sink"))
ggplot(d, aes(N, lam, colour = kind)) +
  geom_hline(yintercept = 1, colour = col_ref, linetype = "dashed") +
  geom_line(linewidth = 1) +
  scale_colour_manual(values = c("true sink" = col_true, "pseudo-sink" = col_pseudo)) +
  labs(x = "Density", y = "Local growth rate", colour = NULL,
       title = "A pseudo-sink is above one until immigration crowds it")

## ---- fig-trap ----
set.seed(409)
q <- runif(12, 0.4, 1.6); pref <- pmin(pmax(q + rnorm(12, 0, 0.12), 0.2), 1.7)
hab <- data.frame(quality = q, preference = pref, type = "matched")
hab <- rbind(hab, data.frame(quality = 0.6, preference = 1.55, type = "trap"))
ggplot(hab, aes(quality, preference, colour = type)) +
  geom_abline(slope = 1, intercept = 0, colour = col_ref, linetype = "dotted") +
  geom_vline(xintercept = 1, colour = col_ref, linetype = "dashed") +
  geom_point(size = 3) +
  annotate("text", x = 0.62, y = 1.62, label = "trap", colour = col_true, size = 3.6) +
  scale_colour_manual(values = c("matched" = col_pseudo, "trap" = col_true), guide = "none") +
  labs(x = "Habitat quality (local growth rate)", y = "Attractiveness to settlers",
       title = "A trap is preferred but poor")
