# The gallery figures below show the data; the significance testing lives in the
# emmeans table above. No per-panel brackets are drawn, so no per-analyte
# contrasts are computed here.
# Complete the palette for any group the config does not name, so a design
# other than 2x2 does not fail on a missing colour.
OKABE_ITO_SEQ <- c("#0072B2", "#E69F00", "#009E73", "#D55E00",
"#CC79A7", "#56B4E9", "#F0E442", "#999999")
group_levels_all <- levels(dat_long$Group)
GROUP_COLORS <- setNames(OKABE_ITO[group_levels_all], group_levels_all)
if (any(is.na(GROUP_COLORS))) {
GROUP_COLORS[is.na(GROUP_COLORS)] <-
rep(OKABE_ITO_SEQ, length.out = sum(is.na(GROUP_COLORS)))
}
make_faceted_plot <- function(plot_dat, plot_type = c("dot", "bar", "violin", "box")) {
plot_type <- match.arg(plot_type)
p <- ggplot(plot_dat, aes(x = Group, y = log10_value)) +
scale_color_manual(values = GROUP_COLORS) +
scale_y_continuous(breaks = pretty_breaks(5), labels = math_format(10^.x)) +
labs(y = "log\u2081\u2080 concentration (pg/mL)", x = NULL) +
theme_bw(base_size = 11, base_family = "sans") +
theme(
legend.position = "none",
axis.line = element_line(linewidth = 0.6),
axis.ticks = element_line(linewidth = 0.6),
axis.text.x = element_text(angle = 30, hjust = 1, size = 9),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text = element_text(face = "bold", size = 10),
strip.background = element_rect(fill = "grey95", color = "grey70")
)
# `fill` is only mapped by the bar/violin/box variants; adding the fill scale
# unconditionally makes ggplot warn about a scale with no matching data.
if (plot_type != "dot") {
p <- p + scale_fill_manual(values = GROUP_COLORS)
}
if (plot_type == "dot") {
p <- p +
geom_quasirandom(aes(color = Group), width = 0.15, alpha = 0.8, size = 1.5) +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
width = 0.18, linewidth = 0.4)
} else if (plot_type == "bar") {
p <- p +
stat_summary(aes(fill = Group), fun = mean, geom = "col",
width = 0.6, alpha = 0.45, color = NA) +
geom_quasirandom(aes(color = Group), width = 0.15, size = 1.5, alpha = 0.9) +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
width = 0.18, linewidth = 0.5)
} else if (plot_type == "violin") {
p <- p +
geom_violin(aes(fill = Group, color = Group),
width = 0.8, alpha = 0.15, trim = FALSE, linewidth = 0.6) +
geom_quasirandom(aes(color = Group), width = 0.15, size = 1.5, alpha = 0.9)
} else if (plot_type == "box") {
p <- p +
geom_boxplot(aes(fill = Group, color = Group),
width = 0.6, alpha = 0.15, outlier.shape = NA, linewidth = 0.6) +
geom_quasirandom(aes(color = Group), width = 0.15, size = 1.5, alpha = 0.9)
}
p + facet_wrap(~Analyte, scales = "free_y", ncol = 4)
}
write_plot <- function(p, name) {
ggsave(file.path(OUTPUT_DIR, name),
plot = p, width = 14, height = 8, dpi = 600,
device = "tiff", compression = "lzw")
}