# Original synthetic example, code and data: CC0-1.0.
library(ggplot2)
set.seed(20261118)
d <- expand.grid(x = seq(0, 10, by = 0.5), replicate = 1:2,
                 group = c("A", "B"))
d$group <- factor(d$group, levels = c("A", "B"))
d$y <- 3 + 0.45 * d$x + ifelse(d$group == "B", 0.8, 0) +
  rnorm(nrow(d), sd = 2)
write.csv(d, "synthetic.csv", row.names = FALSE)
palette <- c(A = "#0072B2", B = "#D55E00")
make_plot <- function(match_fill = FALSE) {
  p <- ggplot(d, aes(x, y, colour = group, linetype = group)) +
    geom_point(aes(shape = group), size = 1, alpha = 0.45) +
    scale_colour_manual(name = "Group", values = palette) +
    scale_linetype_manual(name = "Group", values = c(A = "solid", B = "dashed")) +
    scale_shape_manual(name = "Group", values = c(A = 16, B = 17)) +
    labs(x = "Input (arbitrary units)", y = "Response (arbitrary units)",
         caption = "Synthetic data; pointwise 95% mean-response intervals") +
    theme_classic(base_size = 9) +
    theme(legend.position = "top", plot.caption = element_text(size = 7),
          plot.margin = margin(6, 7, 6, 6))
  if (match_fill) {
    p <- p + geom_smooth(aes(fill = group), method = "lm", formula = y ~ x,
                         se = TRUE, level = 0.95, alpha = 0.22, linewidth = 0.65) +
      scale_fill_manual(name = "Group", values = palette)
  } else {
    p <- p + geom_smooth(method = "lm", formula = y ~ x,
                         se = TRUE, level = 0.95, alpha = 0.22, linewidth = 0.65)
  }
  p
}
before <- make_plot(FALSE)
after <- make_plot(TRUE)
# The second layer is the smoother; style edits must preserve its estimates.
b <- ggplot_build(before)$data[[2]]
a <- ggplot_build(after)$data[[2]]
stopifnot(isTRUE(all.equal(b[c("x", "y", "ymin", "ymax", "group")],
                           a[c("x", "y", "ymin", "ymax", "group")],
                           check.attributes = FALSE)),
          length(unique(b$fill)) == 1L, length(unique(a$fill)) == 2L,
          all(a$alpha == 0.22), nrow(d) == 84L)
# Independently verify ggplot's pointwise mean confidence intervals with predict.lm.
for (id in 1:2) {
  rows <- a[a$group == id, ]
  fit <- lm(y ~ x, data = d[d$group == levels(d$group)[id], ])
  ci <- predict(fit, data.frame(x = rows$x), interval = "confidence", level = .95)
  stopifnot(max(abs(rows$y - ci[, "fit"])) < 1e-9,
            max(abs(rows$ymin - ci[, "lwr"])) < 1e-9,
            max(abs(rows$ymax - ci[, "upr"])) < 1e-9)
}
ggsave("before.png", before, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.png", after, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.pdf", after, width = 101.6, height = 76.2, units = "mm", bg = "white")
write.csv(a[c("x", "y", "ymin", "ymax", "group", "colour", "fill", "alpha")],
          "computed-smooth.csv", row.names = FALSE)
capture.output(sessionInfo(), file = "session-info.txt")
cat("PASS: 84 synthetic observations; 2 group models; 160 evaluated rows; unchanged fits and bounds; independent predict.lm matches.\n")
