# Life-history trade-offs in R
# https://tidyecology.com/posts/life-history-trade-offs/
#
# 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-07-01

## ---- 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_fec <- "#b5651d"; col_sur <- "#3a6b52"; col_fit <- "#333333"; col_ref <- "#555555"

fmax <- 6; smax <- 0.85
fec <- function(E) fmax * E^0.7
sur <- function(E) smax * (1 - E)^0.6
R0  <- function(E) fec(E) / (1 - sur(E))
Es <- seq(0.01, 0.99, 0.001); Estar <- Es[which.max(R0(Es))]; R0star <- max(R0(Es))
R0_semel <- fec(1)
juv <- 0.1; adult <- 0.8; cole_extra <- adult/juv

## ---- tradeoff ----
fmax <- 6; smax <- 0.85
fec <- function(E) fmax * E^0.7          # fecundity rises with effort
sur <- function(E) smax * (1 - E)^0.6    # adult survival falls with effort
R0  <- function(E) fec(E) / (1 - sur(E)) # lifetime output for a simple iteroparous life
c(effort_low = R0(0.2), effort_mid = R0(0.45), effort_high = R0(0.9))

## ---- optimum ----
Es <- seq(0.01, 0.99, 0.001)
Estar <- Es[which.max(R0(Es))]
c(optimal_effort = Estar, R0_at_optimum = R0(Estar), R0_if_semelparous = fec(1))

## ---- fig-tradeoff ----
Eg <- seq(0.02, 0.98, 0.01)
d <- rbind(data.frame(E = Eg, y = fec(Eg)/max(fec(Eg)), v = "fecundity (scaled)"),
           data.frame(E = Eg, y = sur(Eg)/smax,          v = "adult survival (scaled)"),
           data.frame(E = Eg, y = R0(Eg)/R0star,         v = "lifetime output (scaled)"))
ggplot(d, aes(E, y, colour = v)) +
  geom_line(linewidth = 1) +
  geom_vline(xintercept = Estar, colour = col_ref, linetype = "dashed") +
  scale_colour_manual(values = c("fecundity (scaled)" = col_fec, "adult survival (scaled)" = col_sur,
                                 "lifetime output (scaled)" = col_fit)) +
  labs(x = "Reproductive effort", y = "Scaled to maximum", colour = NULL,
       title = "The best reproductive effort is intermediate")

## ---- cole ----
juv <- 0.1; adult <- 0.8
c(cole_naive_extra = 1, corrected_extra = adult / juv)

## ---- fig-cole ----
ra <- seq(1, 12, 0.1)
ggplot(data.frame(ratio = ra, extra = ra), aes(ratio, extra)) +
  geom_hline(yintercept = 1, colour = col_ref, linetype = "dashed") +
  geom_line(colour = col_sur, linewidth = 1) +
  annotate("text", x = 3, y = 1.5, label = "Cole's naive answer", hjust = 0, colour = col_ref, size = 3.4) +
  labs(x = "Adult survival / juvenile survival", y = "Extra offspring an annual needs",
       title = "Low juvenile survival is what makes perennials pay")
