# First flowering dates and sampling effort
# https://tidyecology.com/posts/first-flowering-date-and-sampling/
#
# 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 ----
library(ggplot2)
forest <- "#275139"; gold <- "#cda23f"; terra <- "#b5534e"; mown <- "#2f8f63"
theme_te <- function(base = 13) {
  theme_minimal(base_size = base) +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = "#e7e6da", linewidth = 0.3),
          axis.title = element_text(colour = "#46604a"),
          axis.text  = element_text(colour = "#2c3a31"),
          plot.title = element_text(colour = "#16241d", face = "bold"),
          plot.subtitle = element_text(colour = "#5d6b61"),
          plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
          panel.background = element_rect(fill = "#f5f4ee", colour = NA),
          legend.position = "bottom",
          legend.title = element_text(colour = "#46604a"))
}
theme_set(theme_te())

## ---- pop ----
mu <- 130; sdev <- 12
draw_pop <- function(n) round(rnorm(n, mu, sdev))   # n monitored plants, fixed phenology

## ---- effort-sim ----
set.seed(202)
efforts <- c(5, 15, 50, 150, 500)
R <- 4000
sim <- lapply(efforts, function(n) {
  m <- replicate(R, { x <- draw_pop(n); c(min(x), mean(x), median(x)) })
  list(first = m[1, ], mean = m[2, ], median = m[3, ])
})
res <- data.frame(
  effort = efforts,
  first  = sapply(sim, function(s) mean(s$first)),
  mean   = sapply(sim, function(s) mean(s$mean)),
  median = sapply(sim, function(s) mean(s$median)))
res$depth <- (mu - res$first) / sdev            # first date's depth below the mean, in SDs
round(res, 2)

## ---- effort-store ----
f_lo  <- round(res$first[res$effort == 5], 1)
f_hi  <- round(res$first[res$effort == 500], 1)
adv   <- round(f_lo - f_hi, 1)
mrange <- round(diff(range(res$mean)), 1)
medrange <- round(diff(range(res$median)), 1)
d_lo <- round(res$depth[res$effort == 5], 2)
d_hi <- round(res$depth[res$effort == 500], 2)

## ---- fig-effort ----
long <- rbind(
  data.frame(effort = res$effort, day = res$first,  metric = "first date"),
  data.frame(effort = res$effort, day = res$mean,   metric = "mean date"),
  data.frame(effort = res$effort, day = res$median, metric = "median date"))
ggplot(long, aes(effort, day, colour = metric)) +
  geom_line(linewidth = 0.9) + geom_point(size = 2.4) +
  scale_x_log10(breaks = efforts) +
  scale_colour_manual(values = c("first date" = terra, "mean date" = forest,
                                 "median date" = gold)) +
  labs(x = "plants monitored (log scale)", y = "day of year", colour = NULL)

## ---- fig-mindist ----
pick <- match(c(5, 50, 500), efforts)
dens <- do.call(rbind, Map(function(i, lab)
  data.frame(first = sim[[i]]$first, effort = lab),
  pick, factor(c("5 plants", "50 plants", "500 plants"),
               levels = c("5 plants", "50 plants", "500 plants"))))
ggplot(dens, aes(first, fill = effort, colour = effort)) +
  geom_density(alpha = 0.35, linewidth = 0.6) +
  geom_vline(xintercept = mu, linetype = 2, colour = "#16241d") +
  annotate("text", x = mu, y = 0.03, label = "mean date", hjust = -0.05,
           vjust = -0.4, angle = 90, size = 3.2, colour = "#16241d") +
  scale_fill_manual(values = c("5 plants" = gold, "50 plants" = mown,
                               "500 plants" = forest)) +
  scale_colour_manual(values = c("5 plants" = gold, "50 plants" = mown,
                                 "500 plants" = forest)) +
  labs(x = "first observed date (day of year)", y = "density", fill = NULL, colour = NULL) +
  guides(colour = "none")
