# Original example: CC0-1.0.
library(ggplot2)
d <- data.frame(group = factor(c(rep("A", 5), rep("B", 9)), levels = c("A", "B")),
                value = c(1, 2, 2, 2, 20, 1, 2, 3, 4, 5, 6, 7, 8, 20))
base <- ggplot(d, aes(group, value)) +
  scale_x_discrete(labels = c(A = "A (n = 5)", B = "B (n = 9)")) +
  scale_y_continuous(breaks = seq(0, 20, 5)) +
  labs(x = "Group", y = "Response (arbitrary units)") +
  theme_classic(base_size = 11) +
  theme(plot.caption = element_text(size = 8), plot.margin = margin(8, 8, 8, 8))
before <- base + geom_boxplot(width = .45, fill = "white", outlier.colour = "#D55E00") +
  labs(caption = "Default boxplot: whiskers use 1.5 IQR")
hidden <- base + geom_boxplot(width = .45, fill = "white", outlier.shape = NA)
after <- hidden +
  geom_point(position = position_jitter(width = .12, height = 0, seed = 15),
             size = 2, colour = "#17665c") +
  labs(caption = "Boxplot + every observation; horizontal jitter only")
discarded <- base + geom_boxplot(outliers = FALSE)
b <- ggplot_build(before)
a <- ggplot_build(after)
cols <- c("ymin", "lower", "middle", "upper", "ymax")
expected <- data.frame(ymin = c(2, 1), lower = c(2, 3), middle = c(2, 5),
                       upper = c(2, 7), ymax = c(2, 8))
stopifnot(isTRUE(all.equal(b$data[[1]][cols], expected, check.attributes = FALSE)),
          identical(b$data[[1]][cols], a$data[[1]][cols]),
          identical(b$data[[1]]$outliers, list(c(1, 20), 20)),
          nrow(a$data[[2]]) == nrow(d), identical(a$data[[2]]$y, d$value))
summary <- data.frame(group = levels(d$group), n = c(5, 9), expected,
                      IQR = c(0, 4), lower_fence = c(2, -3), upper_fence = c(2, 13),
                      lower_whisker_length = c(0, 2), upper_whisker_length = c(0, 1),
                      outlier_values = c("1;20", "20"))
yrange <- function(plot) ggplot_build(plot)$layout$panel_scales_y[[1]]$range$range
ranges <- rbind(default = yrange(before), hidden = yrange(hidden),
                 discarded = yrange(discarded), raw_overlay = yrange(after))
colnames(ranges) <- c("minimum", "maximum")
stopifnot(identical(unname(ranges["default", ]), c(1, 20)),
          identical(unname(ranges["hidden", ]), c(1, 20)),
          identical(unname(ranges["discarded", ]), c(1, 8)),
          identical(unname(ranges["raw_overlay", ]), c(1, 20)))
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, "boxplot-summary.csv", row.names = FALSE)
write.csv(data.frame(option = rownames(ranges), ranges), "range-check.csv", row.names = FALSE)
capture.output(sessionInfo(), file = "session-info.txt")
print(summary)
print(ranges)
cat("PASS:14 values; A has zero-length whiskers; B ends at1/8; summaries unchanged; all raw y preserved; hide/discard ranges checked.\n")
