I created a multi-facet plot with 4 groups of 3 bars each in every panel using ggplot2
.
I then ran a separate test to see if there are statistically significant differences between each combination of the bar categories in each age group and in each facet, which produced a separate data.frame p.val.df
. I now need to figure out how to get significance bars to appear between the bars, just like in this answer but additionally within each age group. This is where I am running aground. Since I already have my p-values, I don't need to calculate them using geom_signif()
of the ggsignif
package, but would rather just use geom_bracket
of ggpubr
package to plot them. But any way you could make it work is acceptable.
Here's the data and code:
library(ggplot2)
library(ggpubr)
# Main data
df <- data.frame(
factor(rep(c("A1", "A2"), each = 12), levels = c("A1", "A2")),
factor(rep(c("G", "M", "B"), each = 4), levels = c("G", "M", "B")),
factor(rep(c("0-2", "3-5", "6-12", "13-17"), 6), levels = c("0-2", "3-5", "6-12", "13-17")),
c(160, 162, 169, 108, 110, 111, 76, 73, 76, 45, 41, 38, 175,
177, 173, 174, 167, 172, 176, 162, 166, 143, 130, 143))
colnames(df) <- c("Class", "Type", "Age", "Coefficient")
# Intergroup difference significance
p.val.df <- data.frame(
factor(rep(c("A1", "A2"), each = 12), levels = c("A1", "A2")),
factor(rep(c("G", "M", "G"), each = 4), levels = c("G", "M")),
factor(rep(c("B", "B", "M"), each = 4), levels = c("B", "M")),
factor(rep(c("0-2", "3-5", "6-12", "13-17"), 6), levels = c("0-2", "3-5", "6-12", "13-17")),
c(0.635, 0.584, 0.268, 0.051, 0.163, 0.779, 0.302, 0.361, 0.055, 0.425, 0.998, 0.055,
0.707, 0.230, 0.000, 0.002, 0.418, 0.313, 0.211, 0.037, 0.675, 0.764, 0.011, 0.881))
colnames(p.val.df) <- c("Class", "Type1", "Type2", "Age", "p.value")
# Plotting
ggplot(df, aes(x = Age, y = Coefficient, fill = Type)) +
geom_bar(position = "dodge", stat = "identity") +
labs(y = "Coefficient", x = "Age", fill = "Type") +
facet_wrap( ~ Class, scales = "free") +
expand_limits(y = 300) +
theme_classic() +
### NOT SURE HOW TO PROCEED HERE
geom_bracket(
data = p.val.df, y.position = 250, step.increase = 0.1,
aes(xmin = Type1, xmax = Type2, label = signif(p.value, 2)))