# Life tables and population growth in R
# https://tidyecology.com/posts/life-tables-and-lambda/
#
# 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-05-13

## ---- setup ----
library(ggplot2)
library(dplyr)

te_ink <- "#16241d"; te_forest <- "#275139"; te_mown <- "#2f8f63"
te_aband <- "#b5534e"; te_faint <- "#5d6b61"; te_line <- "#dad9ca"; te_paper <- "#f5f4ee"
theme_te <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(text = element_text(colour = "#2c3a31"),
          plot.title = element_text(colour = te_ink, face = "bold", size = base_size * 1.15),
          plot.subtitle = element_text(colour = te_faint, size = base_size * 0.95),
          axis.title = element_text(colour = "#46604a"), axis.text = element_text(colour = te_faint),
          panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = te_line, linewidth = 0.3),
          plot.background = element_rect(fill = te_paper, colour = NA),
          panel.background = element_rect(fill = te_paper, colour = NA),
          legend.key = element_rect(fill = te_paper, colour = NA))
}

age <- 0:5
px  <- c(0.50, 0.60, 0.70, 0.70, 0.50, 0.00)   # survival from age x to x+1
mx  <- c(0.00, 0.00, 1.50, 2.00, 1.70, 1.00)   # female offspring per female at age x
lx  <- cumprod(c(1, head(px, -1)))             # survivorship to the start of age x
round(lx, 4)

## ---- life-table ----
lxmx <- lx * mx
R0   <- sum(lxmx)                 # net reproductive rate
Tgen <- sum(age * lxmx) / R0      # cohort generation time (mean age of mothers)
data.frame(x = age, px = px, lx = round(lx, 4), mx = mx,
           lxmx = round(lxmx, 4), xlxmx = round(age * lxmx, 4))

## ---- r-approx ----
r_approx <- log(R0) / Tgen
c(r_approx = r_approx, lambda_approx = exp(r_approx))

## ---- euler-lotka ----
euler <- function(r) sum(lxmx * exp(-r * age)) - 1
r_exact <- uniroot(euler, interval = c(-1, 1), tol = 1e-12)$root
c(r_exact = r_exact, lambda = exp(r_exact), residual = euler(r_exact))

## ---- fig-survivorship ----
ggplot(data.frame(age = age, lx = lx), aes(age, lx)) +
  geom_line(colour = te_forest, linewidth = 1) +
  geom_point(colour = te_forest, size = 2.6) +
  scale_y_log10(limits = c(0.05, 1)) +
  labs(title = "Survivorship schedule on a log scale",
       subtitle = "A straight log-line is Type II; convex is Type I, concave is Type III",
       x = "Age x (years)", y = expression(l[x] ~ "(log scale)")) +
  theme_te()

## ---- fig-reproduction ----
ggplot(data.frame(age = age, lxmx = lxmx), aes(factor(age), lxmx)) +
  geom_col(fill = te_mown, width = 0.68) +
  geom_vline(xintercept = Tgen + 1, linetype = "dashed", colour = te_aband) +
  labs(title = "Net reproductive contribution by age",
       subtitle = sprintf("Bars sum to R0 = %.2f; their mean age is generation time T = %.2f", R0, Tgen),
       x = "Age x (years)", y = expression(l[x] * m[x])) +
  theme_te()
