# Chapter 9, part A: Multivariate methods for heterogeneous data
# (multidimensional scaling, supplementary information and batch effects, correspondence analysis).
#
# Reproduces the computed figures 9.1 to 9.25 of Holmes and Huber (2019), Modern Statistics for Modern
# Biology, Chapter 9. Part B (ch09_heterogeneous_data_b.R) covers Figures 9.27 to 9.41.
# Written for the course "Statistical Methods for Modern Biology"; the analyses follow the book's steps,
# the code is my own.
#
# Figures not reproduced here:
#   * The chapter's opening photograph and the "devil" margin icons: photographs/icons, not data.
#   * Figure 9.26 (the cell-type tree of Moignard et al.): a hand-drawn schematic. (Part B.)
# Figures 9.19 and 9.21 are screenshots of interactive 3-d plots in the book; here they are redrawn as
# static 3-d scatterplots of the same computed coordinates.
#
# Run:   Rscript ch09_heterogeneous_data_a.R            (all figures of part A)
#        Rscript ch09_heterogeneous_data_a.R fig_9_10   (one figure)

this_dir <- local({
  a <- grep("^--file=", commandArgs(FALSE), value = TRUE)
  if (length(a)) dirname(normalizePath(sub("^--file=", "", a[1]))) else getwd()
})
source(file.path(this_dir, "common.R"))

suppressPackageStartupMessages({
  library(dplyr)
  library(ggrepel)
  library(patchwork)
  library(ade4)
  library(vegan)
})

CACHE <- file.path(CODE_DIR, "cache")
dir.create(CACHE, showWarnings = FALSE)
cached <- function(name, expr) {
  f <- file.path(CACHE, paste0("ch09-", name, ".rds"))
  if (file.exists(f)) return(readRDS(f))
  val <- force(expr)
  saveRDS(val, f)
  val
}

# ===================================================================================================
# Helpers
# ===================================================================================================

# Bar chart of eigenvalues (a "screeplot"); negative eigenvalues are drawn below zero in red.
scree_plot <- function(values, m = length(values), ylab = "eigenvalue", pct = FALSE) {
  v <- values[seq_len(m)]
  if (pct) v <- 100 * v / sum(values)
  df <- data.frame(k = factor(seq_len(m), levels = seq_len(m)), v = v, neg = v < 0)
  ggplot(df, aes(k, v, fill = neg)) +
    geom_col(width = 0.55, colour = "grey25", linewidth = 0.25) +
    scale_fill_manual(values = c(`FALSE` = COL[["blue"]], `TRUE` = COL[["red"]]), guide = "none") +
    geom_hline(yintercept = 0, linewidth = 0.3) +
    labs(x = "k", y = if (pct) "percentage of total" else ylab)
}

# Classical MDS written out (double centring + eigen-decomposition); the counterpart of cmdscale.
classical_mds <- function(D, k = 2) {
  D <- as.matrix(D)
  n <- nrow(D)
  H <- diag(n) - matrix(1 / n, n, n)
  B <- -0.5 * H %*% (D^2) %*% H
  e <- eigen(B, symmetric = TRUE)
  pos <- pmax(e$values[seq_len(k)], 0)
  list(points = e$vectors[, seq_len(k), drop = FALSE] %*% diag(sqrt(pos), k), eig = e$values, B = B)
}

# Approximate RGB colour of monochromatic light of wavelength w (nm), visible range 380-780 nm.
# Piecewise-linear primaries with an intensity fall-off at the two ends of the spectrum and gamma 0.8.
wavelength_rgb <- function(w) {
  sapply(w, function(x) {
    r <- g <- b <- 0
    if (x >= 380 && x < 440) { r <- (440 - x) / 60; b <- 1 }
    else if (x < 490) { g <- (x - 440) / 50; b <- 1 }
    else if (x < 510) { g <- 1; b <- (510 - x) / 20 }
    else if (x < 580) { r <- (x - 510) / 70; g <- 1 }
    else if (x < 645) { r <- 1; g <- (645 - x) / 65 }
    else if (x <= 780) { r <- 1 }
    f <- if (x < 420) 0.3 + 0.7 * (x - 380) / 40 else if (x > 700) 0.3 + 0.7 * (780 - x) / 80 else 1
    rgb((r * f)^0.8, (g * f)^0.8, (b * f)^0.8)
  })
}

# Scatterplot of labelled points with an equal-aspect frame.
label_map <- function(df, x, y, label, col = COL[["blue"]], fixed = TRUE, size = 3) {
  p <- ggplot(df, aes(.data[[x]], .data[[y]], label = .data[[label]])) +
    geom_point(size = 1.4) +
    geom_text_repel(colour = col, size = size, seed = 1, max.overlaps = 50, segment.size = 0.2)
  if (fixed) p <- p + coord_fixed()
  p
}

# Individuals of a two-group or three-group PCA with convex hulls and the group means
# (supplementary points) drawn as large symbols.
hull_plot <- function(coords, group, labels, eigpct, pal = c("#1b9e77", "#d95f02", "#7570b3")) {
  df <- data.frame(x = coords[, 1], y = coords[, 2], g = factor(group), lab = labels)
  hulls <- df |> group_by(g) |> slice(chull(x, y))
  means <- df |> group_by(g) |> summarise(x = mean(x), y = mean(y))
  ggplot(df, aes(x, y, colour = g, fill = g, shape = g)) +
    geom_hline(yintercept = 0, linetype = 2, linewidth = 0.3) +
    geom_vline(xintercept = 0, linetype = 2, linewidth = 0.3) +
    geom_polygon(data = hulls, alpha = 0.12, linewidth = 0.5, show.legend = FALSE) +
    geom_point(size = 1.8) +
    geom_point(data = means, size = 5) +
    geom_text_repel(aes(label = lab), size = 2.6, seed = 1, max.overlaps = 50, show.legend = FALSE,
                    segment.size = 0.2) +
    scale_colour_manual("day", values = pal) + scale_fill_manual("day", values = pal) +
    scale_shape_manual("day", values = c(16, 17, 15)) +
    coord_fixed() +
    labs(x = sprintf("Dim1 (%.1f%%)", eigpct[1]), y = sprintf("Dim2 (%.1f%%)", eigpct[2]))
}

# Correspondence analysis by the SVD of the standardised residuals (own implementation; the counterpart of
# ade4::dudi.coa, used by the demos).
ca_svd <- function(N) {
  N <- as.matrix(N)
  n <- sum(N); P <- N / n
  r <- rowSums(P); cc <- colSums(P)
  S <- (P - r %o% cc) / sqrt(r %o% cc)
  s <- svd(S)
  k <- sum(s$d > 1e-12)
  list(sv = s$d[seq_len(k)], eig = s$d[seq_len(k)]^2, r = r, c = cc, S = S,
       F = (s$u[, seq_len(k), drop = FALSE] / sqrt(r)) %*% diag(s$d[seq_len(k)], k),   # row principal coords
       G = (s$v[, seq_len(k), drop = FALSE] / sqrt(cc)) %*% diag(s$d[seq_len(k)], k))  # column principal coords
}

# CA biplot in symmetric scaling (rows and columns both in principal coordinates).
ca_biplot <- function(coa, axes = c(1, 2), rows = TRUE, cols = TRUE, rowcol = "#7b4fa0",
                      colcol = "#a0522d", size = 3, repel = TRUE) {
  pe <- 100 * coa$eig / sum(coa$eig)
  p <- ggplot() + geom_hline(yintercept = 0, linetype = 2, linewidth = 0.3) +
    geom_vline(xintercept = 0, linetype = 2, linewidth = 0.3)
  if (rows) {
    dr <- data.frame(x = coa$li[, axes[1]], y = coa$li[, axes[2]], lab = rownames(coa$li))
    p <- p + geom_point(data = dr, aes(x, y), colour = rowcol, size = if (repel) 1.6 else 0.6) +
      (if (repel) geom_text_repel(data = dr, aes(x, y, label = lab), colour = rowcol, size = size, seed = 1,
                                  max.overlaps = 60, segment.size = 0.2)
       else geom_text(data = dr, aes(x, y, label = lab), colour = rowcol, size = size, vjust = -0.4,
                      check_overlap = TRUE))
  }
  if (cols) {
    dc <- data.frame(x = coa$co[, axes[1]], y = coa$co[, axes[2]], lab = rownames(coa$co))
    p <- p + geom_point(data = dc, aes(x, y), colour = colcol, shape = 17, size = 2) +
      geom_text_repel(data = dc, aes(x, y, label = lab), colour = colcol, size = size, seed = 2,
                      max.overlaps = 60, segment.size = 0.2)
  }
  p + coord_fixed() + labs(x = sprintf("Dim%d (%.1f%%)", axes[1], pe[axes[1]]),
                           y = sprintf("Dim%d (%.1f%%)", axes[2], pe[axes[2]]))
}

# ===================================================================================================
# Data
# ===================================================================================================
load_ukraine <- function() {
  e <- new.env(); load(msmb_data("ukrainecities.RData"), envir = e)
  list(D = e$ukraine_dists, coords = as.data.frame(e$ukraine_coords)[, c("city", "lat", "lon")])
}
load_ekman <- function() {
  ekm <- read.table(msmb_data("ekman.txt"), header = TRUE)
  rownames(ekm) <- colnames(ekm)
  dis <- 1 - as.matrix(ekm) - diag(1, ncol(ekm))       # similarity -> dissimilarity, zero diagonal
  list(sim = as.matrix(ekm), D = as.dist(dis), wl = as.numeric(sub("w", "", colnames(ekm))))
}
load_ibd <- function() {
  chip <- readRDS(msmb_data("vsn28Exprd.rds"))
  list(assay = chip[-nrow(chip), ], day = factor(chip[nrow(chip), ]))
}
# Rank-threshold transformation of the book (Section 9.3.1): within each sample, rank the taxa; every
# rank below the threshold is tied at the threshold; then PCA (centred and scaled) of the samples.
rank_threshold <- function(x, threshold = 3000) {
  R <- apply(x, 2, rank)
  R[R < threshold] <- threshold
  threshold - R
}
rankthresh_pca <- function(x, threshold = 3000, nf = 2) {
  dudi.pca(t(rank_threshold(x, threshold)), scannf = FALSE, nf = nf)
}
load_hiv <- function() read.delim2(msmb_data("coccurHIV.txt"), header = TRUE, sep = ",")
load_lakes <- function() { e <- new.env(); load(msmb_data("lakes.RData"), envir = e); list(lakelike = e$lakelike, lakelikeh = e$lakelikeh) }

# ===================================================================================================
# 9.2 Multidimensional scaling
# ===================================================================================================
ukraine_mds <- function() {
  u <- load_ukraine()
  m <- cmdscale(u$D, k = 2, eig = TRUE)
  pts <- m$points
  # fix the arbitrary signs: Luhansk (far east) has the largest first coordinate, Sevastopol the largest second
  if (rownames(pts)[which.max(pts[, 1])] != "Luhansk") pts[, 1] <- -pts[, 1]
  if (rownames(pts)[which.max(pts[, 2])] != "Sevastopol") pts[, 2] <- -pts[, 2]
  list(eig = m$eig, df = data.frame(PCo1 = pts[, 1], PCo2 = pts[, 2], city = rownames(pts)), u = u)
}

fig_9_1 <- function() {
  u <- load_ukraine()
  M <- as.matrix(u$D)
  br <- seq(0, sqrt(max(M)), length.out = 51)^2
  ph <- pheatmap::pheatmap(M, color = colorRampPalette(c("#0057b7", "#ffd700"))(50), breaks = br,
                           treeheight_row = 12, treeheight_col = 12, fontsize = 7, silent = TRUE)
  savefig(ph$gtable, "fig-9-1", ch = 9, width = 6.6, height = 6)
}

fig_9_2 <- function() {
  m <- ukraine_mds()
  savefig(scree_plot(m$eig, 4), "fig-9-2", ch = 9, width = 4, height = 3.4)
}

fig_9_3 <- function() {
  m <- ukraine_mds()
  p <- scree_plot(m$eig) + scale_x_discrete(breaks = c(1, 5, 10, 15, 20, 25, 30))
  savefig(p, "fig-9-3", ch = 9, width = 6, height = 3.4)
}

fig_9_4 <- function() {
  m <- ukraine_mds()
  p <- label_map(m$df, "PCo1", "PCo2", "city", size = 2.6)   # Crimea at the top, as cmdscale may return it
  savefig(p, "fig-9-4", ch = 9, width = 6, height = 4.6)
}

fig_9_5 <- function() {
  m <- ukraine_mds()
  df <- m$df; df$PCo2 <- -df$PCo2
  p <- label_map(df, "PCo1", "PCo2", "city", size = 2.6)
  savefig(p, "fig-9-5", ch = 9, width = 6, height = 4.6)
}

fig_9_6 <- function() {
  u <- load_ukraine()
  p <- label_map(u$coords, "lon", "lat", "city", fixed = FALSE, size = 2.6) +
    labs(x = "longitude (degrees east)", y = "latitude (degrees north)")
  savefig(p, "fig-9-6", ch = 9, width = 6, height = 4.2)
}

fig_9_7 <- function() {
  u <- load_ukraine()
  border <- ggplot2::map_data("world", region = "Ukraine")
  p <- ggplot(u$coords, aes(lon, lat)) +
    geom_polygon(data = border, aes(long, lat, group = group), fill = "#ffd700", colour = "#0057b7",
                 linewidth = 0.4) +
    geom_point(size = 1.2) +
    geom_text_repel(aes(label = city), size = 2.5, seed = 1, max.overlaps = 50, segment.size = 0.2) +
    coord_fixed(1 / cos(48 * pi / 180)) +       # one degree of longitude is cos(48 deg) of a degree of latitude
    labs(x = "longitude (degrees east)", y = "latitude (degrees north)")
  savefig(p, "fig-9-7", ch = 9, width = 6.4, height = 4.6)
}

ekman_mds <- function() {
  ek <- load_ekman()
  m <- cmdscale(ek$D, k = 2, eig = TRUE)
  df <- data.frame(MDS1 = m$points[, 1], MDS2 = m$points[, 2], name = rownames(m$points),
                   col = wavelength_rgb(ek$wl))
  list(eig = m$eig, df = df, ek = ek)
}

colour_map <- function(df, x, y) {
  ggplot(df, aes(.data[[x]], .data[[y]], label = name)) +
    geom_point(colour = df$col, size = 4) +
    geom_text_repel(size = 3, seed = 1, segment.size = 0.2) +
    coord_fixed()
}

fig_9_8 <- function() {
  m <- ekman_mds()
  savefig(scree_plot(m$eig), "fig-9-8", ch = 9, width = 4.4, height = 3.4)
}

fig_9_9 <- function() {
  m <- ekman_mds()
  savefig(colour_map(m$df, "MDS1", "MDS2"), "fig-9-9", ch = 9, width = 5, height = 4.6)
}

# 100 runs of metaMDS for each of k = 1, ..., 4 (each run is itself a best-of-random-starts fit).
nmds_stress_runs <- function() cached("nmds-stress", {
  ek <- load_ekman()
  set.seed(2019)
  sapply(1:4, function(k) replicate(100, metaMDS(ek$D, k = k, autotransform = FALSE, trace = 0)$stress))
})

fig_9_10 <- function() {
  st <- nmds_stress_runs()
  df <- data.frame(stress = as.vector(st), k = factor(rep(1:4, each = nrow(st))))
  p <- ggplot(df, aes(k, stress)) + geom_boxplot(width = 0.5, outlier.size = 0.8, fill = "grey92") +
    labs(x = "number of dimensions k", y = "stress (Kruskal stress-1)")
  savefig(p, "fig-9-10", ch = 9, width = 4.4, height = 3.6)
}

ekman_nmds <- function() cached("ekman-nmds2", {
  ek <- load_ekman()
  set.seed(1)
  metaMDS(ek$D, k = 2, autotransform = FALSE, trace = 0)
})

fig_9_11 <- function() {
  fit <- ekman_nmds()
  df <- data.frame(diss = fit$diss, dist = fit$dist, dhat = fit$dhat)
  df <- df[order(df$diss, df$dhat), ]
  r2_nm <- 1 - fit$stress^2
  r2_lin <- cor(df$diss, df$dist)^2
  p <- ggplot(df, aes(diss)) +
    geom_point(aes(y = dist), colour = COL[["blue"]], size = 1.4) +
    geom_step(aes(y = dhat), colour = COL[["red"]], linewidth = 0.7) +
    annotate("text", x = min(df$diss), y = max(df$dist), hjust = 0, vjust = 1, size = 3.2,
             label = sprintf("Non-metric fit, R^2 = %.3f\nLinear fit, R^2 = %.3f", r2_nm, r2_lin)) +
    labs(x = "observed dissimilarity", y = "ordination distance")
  savefig(p, "fig-9-11", ch = 9, width = 4.6, height = 4)
}

fig_9_12 <- function() {
  m <- ekman_mds()
  fit <- ekman_nmds()
  nm <- data.frame(NmMDS1 = fit$points[, 1], NmMDS2 = fit$points[, 2], name = m$df$name, col = m$df$col)
  # align the signs of the nonmetric axes with the classical ones (signs are arbitrary)
  for (j in 1:2) if (cor(nm[[j]], m$df[[j]]) < 0) nm[[j]] <- -nm[[j]]
  p <- (colour_map(m$df, "MDS1", "MDS2") + ggtitle("(a) classical MDS")) +
    (colour_map(nm, "NmMDS1", "NmMDS2") + ggtitle("(b) nonmetric MDS"))
  savefig(p, "fig-9-12", ch = 9, width = 9, height = 4.4)
}

# ===================================================================================================
# 9.3 Supplementary information and batch effects (Phylochip data)
# ===================================================================================================
ibd_pcas <- function() cached("ibd-pcas", {
  d <- load_ibd()
  keep <- d$day != 3
  p12 <- rankthresh_pca(d$assay[, keep])
  p123 <- rankthresh_pca(d$assay)
  mod0 <- model.matrix(~1, data = data.frame(day = d$day))
  combat <- suppressMessages(sva::ComBat(dat = d$assay, batch = d$day, mod = mod0))
  pcb <- rankthresh_pca(combat)
  strip <- function(p) list(li = as.matrix(p$li), eig = p$eig)
  list(p12 = strip(p12), p123 = strip(p123), combat = strip(pcb), day = d$day, keep = keep,
       labels = colnames(d$assay))
})

fviz_scree_pct <- function(eig, m = 10) {
  pct <- 100 * eig / sum(eig)
  df <- data.frame(k = factor(seq_len(m), levels = seq_len(m)), v = pct[seq_len(m)])
  ggplot(df, aes(k, v)) + geom_col(width = 0.6, fill = "#4682b4") +
    geom_line(aes(group = 1)) + geom_point() +
    labs(x = "dimensions", y = "percentage of explained variance")
}

fig_9_13 <- function() {
  P <- ibd_pcas()
  savefig(fviz_scree_pct(P$p12$eig), "fig-9-13", ch = 9, width = 4.4, height = 3.6)
}

fig_9_14 <- function() {
  P <- ibd_pcas()
  pe <- 100 * P$p12$eig / sum(P$p12$eig)
  p <- hull_plot(P$p12$li, droplevels(P$day[P$keep]), P$labels[P$keep], pe)
  savefig(p, "fig-9-14", ch = 9, width = 6, height = 4.4)
}

fig_9_15 <- function() {
  P <- ibd_pcas()
  pe12 <- 100 * P$p12$eig / sum(P$p12$eig)
  pe123 <- 100 * P$p123$eig / sum(P$p123$eig)
  pa <- hull_plot(P$p12$li, droplevels(P$day[P$keep]), P$labels[P$keep], pe12) + ggtitle("(a) days 1 and 2")
  pb <- hull_plot(P$p123$li, P$day, P$labels, pe123) + ggtitle("(b) days 1, 2 and 3")
  savefig(pa + pb, "fig-9-15", ch = 9, width = 11, height = 4.6)
}

fig_9_16 <- function() {
  P <- ibd_pcas()
  savefig(fviz_scree_pct(P$p123$eig), "fig-9-16", ch = 9, width = 4.4, height = 3.6)
}

fig_9_17 <- function() {
  P <- ibd_pcas()
  pe <- 100 * P$combat$eig / sum(P$combat$eig)
  savefig(hull_plot(P$combat$li, P$day, P$labels, pe), "fig-9-17", ch = 9, width = 6, height = 4.6)
}

# Single-cell data of Section 9.3.3 (1000 genes, 747 cells, normalised as in Perraudeau et al. 2017).
normse_pca <- function() cached("normse-pca", {
  se <- readRDS(msmb_data("normse.rds"))
  nv <- SummarizedExperiment::assay(se, "normalizedValues")
  cd <- SummarizedExperiment::colData(se)
  p <- dudi.pca(t(nv), nf = 3, scannf = FALSE)
  list(eig = p$eig, li = as.matrix(p$li), batch = as.character(cd$Batch),
       cluster = as.character(cd$publishedClusters))
})

fig_9_18 <- function() {
  P <- normse_pca()
  savefig(scree_plot(P$eig, 15), "fig-9-18", ch = 9, width = 4.6, height = 3.4)
}

scatter3 <- function(xyz, col, labs, angle = 40, main = "", cex = 0.7, box = TRUE) {
  function() {
    par(mar = c(2.5, 2.5, 2, 1))
    scatterplot3d::scatterplot3d(xyz[, 1], xyz[, 2], xyz[, 3], color = col, pch = 16, cex.symbols = cex,
                                 xlab = labs[1], ylab = labs[2], zlab = labs[3], angle = angle, main = main,
                                 box = box, cex.axis = 0.7, cex.lab = 0.9, tick.marks = TRUE)
  }
}

fig_9_19 <- function() {
  P <- normse_pca()
  bpal <- setNames(grDevices::hcl.colors(length(unique(P$batch)), "Dark 3"), sort(unique(P$batch)))
  cl <- sort(unique(P$cluster))
  cpal <- setNames(c("grey80", "#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E", "#E6AB02",
                     "#A6761D", "#1F78B4", "#B2DF8A", "#FB9A99", "#CAB2D6", "#FF7F00", "#6A3D9A")[seq_along(cl)], cl)
  draw <- function() {
    op <- par(mfrow = c(1, 2)); on.exit(par(op))
    scatter3(P$li, bpal[P$batch], c("PC1", "PC2", "PC3"), main = "(a) coloured by batch")()
    scatter3(P$li, cpal[P$cluster], c("PC1", "PC2", "PC3"), main = "(b) coloured by published cluster")()
  }
  savefig(draw, "fig-9-19", ch = 9, width = 10, height = 5.2)
}

# ===================================================================================================
# 9.4 Correspondence analysis
# ===================================================================================================
hiv_ca <- function() dudi.coa(load_hiv(), nf = 4, scannf = FALSE)

fig_9_20 <- function() {
  ca <- hiv_ca()
  savefig(fviz_scree_pct(ca$eig) + labs(y = "percentage of inertia"), "fig-9-20", ch = 9, width = 4.4, height = 3.6)
}

fig_9_21 <- function() {
  ca <- hiv_ca()
  savefig(scatter3(as.matrix(ca$li[, 1:3]), "#7b4fa0", c("CA1", "CA2", "CA3"), angle = 55, cex = 0.8),
          "fig-9-21", ch = 9, width = 5, height = 4.6)
}

fig_9_22 <- function() {
  ca <- hiv_ca()
  pa <- ca_biplot(ca, c(1, 2), cols = FALSE, size = 2.3, repel = FALSE) + ggtitle("(a) axes 1 and 2")
  pb <- ca_biplot(ca, c(1, 3), cols = FALSE, size = 2.3, repel = FALSE) + ggtitle("(b) axes 1 and 3")
  savefig(pa / pb, "fig-9-22", ch = 9, width = 6.4, height = 8)
}

fig_9_23 <- function() {
  hc <- HairEyeColor[, , 2]
  draw <- function() {
    par(mar = c(2, 3, 1, 1))
    mosaicplot(hc, shade = TRUE, las = 1, type = "pearson", cex.axis = 0.8, main = "")
  }
  savefig(draw, "fig-9-23", ch = 9, width = 5.4, height = 5)
}

fig_9_24 <- function() {
  hc <- as.data.frame.matrix(HairEyeColor[, , 2])
  ca <- dudi.coa(hc, scannf = FALSE, nf = 2)
  savefig(ca_biplot(ca), "fig-9-24", ch = 9, width = 5.6, height = 3.6)
}

# ===================================================================================================
# 9.5 Gradients: the lake data
# ===================================================================================================
fig_9_25 <- function() {
  L <- load_lakes()
  ca <- dudi.coa(L$lakelike, scannf = FALSE, nf = 2)
  pa <- ca_biplot(ca, cols = FALSE, size = 3.4) + ggtitle("(a) locations")
  pb <- ca_biplot(ca, size = 3.4) + ggtitle("(b) locations and plants")
  savefig(pa / pb, "fig-9-25", ch = 9, width = 7, height = 8)
}

# ===================================================================================================
# Data for the browser demonstrations
# ===================================================================================================
export_demo_data <- function() {
  u <- load_ukraine()
  m <- ukraine_mds()
  export_json(list(city = u$coords$city, lat = u$coords$lat, lon = u$coords$lon,
                   D = unname(round(as.matrix(u$D), 3)),
                   ref = list(eig = signif(m$eig[1:4], 8))),
              "ch09-ukraine.json")
  ek <- ekman_mds()
  fit <- ekman_nmds()
  export_json(list(name = ek$df$name, wl = ek$ek$wl, col = unname(ek$df$col),
                   D = unname(as.matrix(ek$ek$D)), ref = list(eig = signif(ek$eig, 8), vegan_stress2 = fit$stress)),
              "ch09-ekman.json")
  L <- load_lakes()
  hc <- HairEyeColor[, , 2]
  plato <- read.table(msmb_data("platof.txt"), header = TRUE)
  cs <- read.csv(msmb_data("colorsentiment.csv"))
  colsent <- unclass(xtabs(cs[, 3] ~ cs[, 2] + cs[, 1]))
  colsent <- round(colsent / 1000)
  tab <- function(M) { M <- as.matrix(M); list(rows = rownames(M), cols = colnames(M), N = matrix(as.numeric(M), nrow(M))) }
  export_json(list(hair = tab(hc), lakes = tab(L$lakelike), lakesh = tab(L$lakelikeh), plato = tab(plato),
                   colours = tab(colsent)),
              "ch09-ca-tables.json")
}

run_figures(list(fig_9_1 = fig_9_1, fig_9_2 = fig_9_2, fig_9_3 = fig_9_3, fig_9_4 = fig_9_4,
                 fig_9_5 = fig_9_5, fig_9_6 = fig_9_6, fig_9_7 = fig_9_7, fig_9_8 = fig_9_8,
                 fig_9_9 = fig_9_9, fig_9_10 = fig_9_10, fig_9_11 = fig_9_11, fig_9_12 = fig_9_12,
                 fig_9_13 = fig_9_13, fig_9_14 = fig_9_14, fig_9_15 = fig_9_15, fig_9_16 = fig_9_16,
                 fig_9_17 = fig_9_17, fig_9_18 = fig_9_18, fig_9_19 = fig_9_19, fig_9_20 = fig_9_20,
                 fig_9_21 = fig_9_21, fig_9_22 = fig_9_22, fig_9_23 = fig_9_23, fig_9_24 = fig_9_24,
                 fig_9_25 = fig_9_25, export_demo_data = export_demo_data))
