# Modularity in ecological networks
# https://tidyecology.com/posts/modularity-in-ecological-networks/
#
# 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-07

## ---- setup ----
library(ggplot2)
library(grid)
te_ink <- "#16241d"; te_body <- "#2c3a31"; te_forest <- "#275139"; te_label <- "#46604a"
te_sage <- "#93a87f"; te_paper <- "#f5f4ee"; te_line <- "#dad9ca"; te_gold <- "#c9b458"
te_rust <- "#b5534e"
theme_te <- function(base = 12) theme_minimal(base_size = base) +
  theme(plot.background = element_rect(fill = te_paper, colour = NA),
        panel.background = element_rect(fill = te_paper, colour = NA),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_line(colour = te_line, linewidth = 0.3),
        axis.text = element_text(colour = te_body), axis.title = element_text(colour = te_label),
        plot.title = element_text(colour = te_ink, face = "bold", size = rel(1.02)),
        plot.subtitle = element_text(colour = te_label, size = rel(0.9)),
        legend.text = element_text(colour = te_body), legend.title = element_text(colour = te_label))

## ---- build-web ----
set.seed(4261)
A0 <- 18L; P0 <- 15L; K <- 3L
poll_grp  <- sort(sample(rep(seq_len(K), length.out = A0)))
plant_grp <- sort(sample(rep(seq_len(K), length.out = P0)))
poll_ab   <- exp(rnorm(A0, 0, 0.4)); plant_ab <- exp(rnorm(P0, 0, 0.4))
lam <- outer(poll_ab, plant_ab) * ifelse(outer(poll_grp, plant_grp, "==") == 1, 1.0, 0.11)
lam <- lam / sum(lam) * 660
W <- matrix(rpois(A0 * P0, lam), A0, P0)
rownames(W) <- paste0("A", seq_len(A0)); colnames(W) <- paste0("P", seq_len(P0))
W <- W[rowSums(W) > 0, colSums(W) > 0, drop = FALSE]
A <- nrow(W); P <- ncol(W); m <- sum(W)
c(pollinators = A, plants = P, interactions = m, connectance = round(sum(W > 0) / (A * P), 3))

## ---- barber-q ----
ki <- rowSums(W); dj <- colSums(W)
Bmat <- W - outer(ki, dj) / m
Qbip <- function(rl, cl) {
  q <- 0
  for (i in seq_len(A)) { same <- which(cl == rl[i]); if (length(same)) q <- q + sum(Bmat[i, same]) }
  q / m
}

## ---- lpawb ----
lpawb <- function(seed) {
  set.seed(seed)
  nl <- A + P
  rl <- sample(seq_len(nl), A, replace = TRUE)
  cl <- sample(seq_len(nl), P, replace = TRUE)
  repeat {
    changed <- FALSE
    for (i in seq_len(A)) {
      labs <- unique(cl); score <- sapply(labs, function(cc) sum(Bmat[i, cl == cc]))
      best <- labs[which.max(score)]; if (best != rl[i]) { rl[i] <- best; changed <- TRUE }
    }
    for (j in seq_len(P)) {
      labs <- unique(rl); score <- sapply(labs, function(cc) sum(Bmat[rl == cc, j]))
      best <- labs[which.max(score)]; if (best != cl[j]) { cl[j] <- best; changed <- TRUE }
    }
    if (!changed) break
  }
  list(rl = rl, cl = cl, Q = Qbip(rl, cl))
}
runs <- lapply(seq_len(40L), function(s) lpawb(1000 + s))
Qs   <- vapply(runs, `[[`, numeric(1), "Q")
best <- runs[[which.max(Qs)]]
alllab <- unique(c(best$rl, best$cl)); map <- setNames(seq_along(alllab), alllab)
rlab <- map[as.character(best$rl)]; clab <- map[as.character(best$cl)]
Qobs <- best$Q; nmod <- length(unique(c(rlab, clab)))
c(Q = round(Qobs, 3), modules = nmod, restart_low = round(min(Qs), 3), restart_high = round(max(Qs), 3))

## ---- roles ----
Adj <- (W > 0) * 1L
node_mod <- c(rlab, clab); N <- A + P
G <- matrix(0L, N, N); G[1:A, (A + 1):N] <- Adj; G[(A + 1):N, 1:A] <- t(Adj)
deg <- rowSums(G)
zc_mat <- t(vapply(seq_len(N), function(v) {
  s <- node_mod[v]; kis <- sum(G[v, node_mod == s]); members <- which(node_mod == s)
  kv <- vapply(members, function(u) sum(G[u, node_mod == s]), numeric(1))
  z  <- if (sd(kv) > 0) (kis - mean(kv)) / sd(kv) else 0
  ct <- vapply(sort(unique(node_mod)), function(t) sum(G[v, node_mod == t]), numeric(1))
  cpar <- if (deg[v] > 0) 1 - sum((ct / deg[v])^2) else 0
  c(z = z, c = cpar)
}, numeric(2)))
role <- ifelse(zc_mat[, "z"] > 2.5 & zc_mat[, "c"] > 0.62, "network hub",
        ifelse(zc_mat[, "z"] > 2.5, "module hub",
        ifelse(zc_mat[, "c"] > 0.62, "connector", "peripheral")))
role_tab <- list(zc = zc_mat, role = role)
n_conn <- sum(role == "connector"); n_periph <- sum(role == "peripheral")
n_hub  <- sum(grepl("hub", role))
c(connector = n_conn, peripheral = n_periph, hub = n_hub)

## ---- fig-modules ----
ro <- order(rlab); co <- order(clab)
Wr <- W[ro, co]; rl2 <- rlab[ro]; cl2 <- clab[co]
dfm <- data.frame(ri = rep(seq_len(A), times = P), ci = rep(seq_len(P), each = A), n = as.vector(Wr))
rblk <- tapply(seq_len(A), rl2, range); cblk <- tapply(seq_len(P), cl2, range)
mods <- intersect(names(rblk), names(cblk))
rects <- do.call(rbind, lapply(mods, function(mm)
  data.frame(xmin = cblk[[mm]][1] - 0.5, xmax = cblk[[mm]][2] + 0.5,
             ymin = rblk[[mm]][1] - 0.5, ymax = rblk[[mm]][2] + 0.5)))
pa <- ggplot(dfm, aes(ci, ri, fill = n)) +
  geom_tile(colour = te_paper, linewidth = 0.3) +
  scale_fill_gradient(low = te_paper, high = te_forest, name = "visits") +
  geom_rect(data = rects, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            inherit.aes = FALSE, fill = NA, colour = te_rust, linewidth = 0.9) +
  scale_y_reverse(breaks = NULL) + scale_x_continuous(breaks = NULL) +
  labs(title = "Modules in the reordered matrix", x = "plants (grouped by module)",
       y = "pollinators (grouped by module)",
       subtitle = sprintf("Q = %.2f, %d modules", Qobs, nmod)) +
  theme_te()
zc <- as.data.frame(role_tab$zc); zc$role <- role_tab$role
zc$type <- ifelse(seq_len(nrow(zc)) <= A, "pollinator", "plant")
pb <- ggplot(zc, aes(c, z, colour = role, shape = type)) +
  geom_hline(yintercept = 2.5, linetype = "dashed", colour = te_label, linewidth = 0.4) +
  geom_vline(xintercept = 0.62, linetype = "dashed", colour = te_label, linewidth = 0.4) +
  geom_point(size = 2.6, alpha = 0.9) +
  scale_colour_manual(values = c(connector = te_forest, peripheral = te_gold,
                                 `module hub` = te_rust, `network hub` = te_ink), name = "role") +
  scale_shape_manual(values = c(pollinator = 16, plant = 17), name = "node") +
  labs(title = "Module roles (c-z space)", x = "participation coefficient c",
       y = "within-module degree z",
       subtitle = "thresholds: c = 0.62 (connector), z = 2.5 (hub)") +
  theme_te()
grid.newpage(); pushViewport(viewport(layout = grid.layout(1, 2)))
print(pa, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(pb, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))

## ---- null-q ----
set.seed(99)
nnull <- 199L
nulls <- r2dtable(nnull, ki, dj)
Qnull <- vapply(seq_len(nnull), function(b) {
  Wn <- nulls[[b]]; kn <- rowSums(Wn); dn <- colSums(Wn); Bn <- Wn - outer(kn, dn) / sum(Wn)
  Aq <- nrow(Wn); Pq <- ncol(Wn)
  QN <- function(rl, cl) { q <- 0; for (i in seq_len(Aq)) { s <- which(cl == rl[i]); if (length(s)) q <- q + sum(Bn[i, s]) }; q / sum(Wn) }
  lp <- function(seed) {
    set.seed(seed); nl <- Aq + Pq; rl <- sample(nl, Aq, TRUE); cl <- sample(nl, Pq, TRUE)
    repeat { ch <- FALSE
      for (i in seq_len(Aq)) { L <- unique(cl); sc <- sapply(L, function(cc) sum(Bn[i, cl == cc])); bb <- L[which.max(sc)]; if (bb != rl[i]) { rl[i] <- bb; ch <- TRUE } }
      for (j in seq_len(Pq)) { L <- unique(rl); sc <- sapply(L, function(cc) sum(Bn[rl == cc, j])); bb <- L[which.max(sc)]; if (bb != cl[j]) { cl[j] <- bb; ch <- TRUE } }
      if (!ch) break }
    QN(rl, cl)
  }
  max(vapply(1:6, function(s) lp(500 * b + s), numeric(1)))
}, numeric(1))
z_mod <- (Qobs - mean(Qnull)) / sd(Qnull)
p_mod <- (1 + sum(Qnull >= Qobs)) / (nnull + 1)
c(null_mean = round(mean(Qnull), 3), null_sd = round(sd(Qnull), 3),
  observed = round(Qobs, 3), z = round(z_mod, 2), p = round(p_mod, 3))

## ---- fig-null ----
dn <- data.frame(Q = Qnull)
pc <- ggplot(dn, aes(Q)) +
  geom_histogram(bins = 22, fill = te_sage, colour = te_paper, linewidth = 0.2) +
  geom_vline(xintercept = Qobs, colour = te_rust, linewidth = 1) +
  annotate("text", x = Qobs, y = Inf, label = sprintf("observed Q = %.2f", Qobs),
           hjust = 1.05, vjust = 1.6, colour = te_rust, size = 3.4) +
  labs(title = "Observed modularity vs null", x = "Q of Patefield-randomised webs", y = "count",
       subtitle = sprintf("null mean %.2f, z = %.1f, p = %.3f", mean(Qnull), z_mod, p_mod)) +
  theme_te()
dq <- data.frame(run = seq_along(Qs), Q = sort(Qs)); dq$best <- dq$Q == max(dq$Q)
pd <- ggplot(dq, aes(run, Q)) +
  geom_point(aes(colour = best), size = 2.3) +
  scale_colour_manual(values = c(`FALSE` = te_sage, `TRUE` = te_rust), guide = "none") +
  geom_hline(yintercept = max(dq$Q), linetype = "dashed", colour = te_rust, linewidth = 0.4) +
  labs(title = "Modularity across random restarts", x = "restart (sorted)", y = "Q found",
       subtitle = sprintf("best of %d kept; range %.2f to %.2f", length(Qs), min(Qs), max(Qs))) +
  theme_te()
grid.newpage(); pushViewport(viewport(layout = grid.layout(1, 2)))
print(pc, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(pd, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
