# Original synthetic values, code and figures: CC0-1.0.
library(ggplot2)
d <- data.frame(group = factor(rep(c("A", "B"), each = 5), levels = c("A", "B")),
                value = c(8, 9, 10, 11, 12, 12, 13, 14, 15, 16))
parts <- split(d$value, d$group)
summary <- data.frame(group = factor(names(parts), levels = levels(d$group)),
                      n = vapply(parts, length, integer(1)),
                      mean = vapply(parts, mean, numeric(1)),
                      sd = vapply(parts, sd, numeric(1)))
summary$lower <- summary$mean - summary$sd
summary$upper <- summary$mean + summary$sd
# Independent hand-check: both groups have sum of squared deviations 10.
stopifnot(all(summary$n == 5), identical(unname(summary$mean), c(10, 14)),
          max(abs(summary$sd - sqrt(10/4))) < 1e-12,
          all(vapply(parts, function(x) sum((x - mean(x))^2), numeric(1)) == 10))
before <- ggplot(d, aes(group, value)) +
  geom_point(position = position_jitter(width = .06, height = 0, seed = 11),
              size = 2, colour = "#17665c") +
  scale_x_discrete(labels = c(A = "A (n = 5)", B = "B (n = 5)")) +
  scale_y_continuous(limits = c(7, 17), breaks = seq(8, 16, 2)) +
  labs(x = "Group", y = "Response (arbitrary units)",
       caption = "Synthetic data; small dots are observations") +
  theme_classic(base_size = 10) +
  theme(plot.caption = element_text(size = 7), plot.margin = margin(7, 8, 7, 7))
after <- before +
  geom_errorbar(data = summary, aes(x = group, ymin = lower, ymax = upper),
                  inherit.aes = FALSE, width = .16, linewidth = .6) +
  geom_point(data = summary, aes(x = group, y = mean), inherit.aes = FALSE,
               shape = 23, size = 3.5, fill = "#D55E00", colour = "black") +
  labs(caption = "Synthetic data; diamonds: means; error bars: +/-1 sample SD")
b <- ggplot_build(before)
a <- ggplot_build(after)
stopifnot(nrow(a$data[[1]]) == 10L,
          identical(a$data[[1]][c("x", "y")], b$data[[1]][c("x", "y")]),
          all(a$data[[1]]$y == d$value),
          max(abs(a$data[[2]]$ymin - summary$lower)) < 1e-12,
          max(abs(a$data[[2]]$ymax - summary$upper)) < 1e-12,
          all(as.numeric(a$data[[2]]$x) == as.numeric(a$data[[3]]$x)))
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(d, "synthetic.csv", row.names = FALSE)
write.csv(summary, "summary.csv", row.names = FALSE)
capture.output(sessionInfo(), file = "session-info.txt")
print(summary)
cat("PASS: 10 observations; n=5 in each group; means10/14; sampleSD=sqrt(2.5); y values and seeded x jitter unchanged; interval endpoints and group centres match.\n")
