I have a data frame including multiple factors. I used ggboxplot to get a box plot with comparisons for different categories. I am not satisfied with the x axis labels. I tried
This gets you close to what you're looking for (I couldn't figure out the line separator). You may also have to play around with the positioning of the labels to get them just right, as well as sizes.
ggboxplot(df, x = "group",y = "rating",
color = "group", palette = "simpsons",
add = "jitter", facet.by="country", legend="none", ylab="Rating") +
scale_x_discrete(labels=rep(c("F","M"),4)) +
theme(strip.text.x=element_text(size=10, color="red", face="bold.italic"),
axis.title.x = element_blank(),
plot.margin=unit(c(2,2,15,2), "mm")) +
stat_compare_means(method = "t.test",comparisons = my_comparisons,
label.y = 110, label = "p.signif") +
coord_cartesian(ylim=c(20,120), xlim=c(1,4), clip="off") +
annotate("text", x=1.5, y=0, label=c("","","Private","Private")) +
annotate("text", x=3.5, y=0, label=c("","","Public","Public")) +
annotate("text", x=0.5, y=10, label=c("","","Sex",""), hjust=1) +
annotate("text", x=0.5, y=0, label=c("","","School",""), hjust=1)
Additions include scale_x_discrete()
to change x-axis labels, plot.margin
and coord_cartesian
to allow annotations outside the plot area, and annotate
for each annotation, where the labels for each facet panel are given as a vector, with blanks for panels which shouldn't get labels.
There may be a cleaner way to do this, but the faceted nature of the plot means that annotations get replicated across facets which you don't want in this case.