I am generating a faceted plot with different groups and I want to compare the mean of different groups to a control sample. I have a wide range of values, and want to position the "significance label" programmatically on top of each boxplot.
Here is some toy data.
library(ggpubr)
ggboxplot(mtcars, x = "am", y = "mpg") +
facet_wrap(~vs) +
stat_compare_means(aes(label = ..p.signif..),
method = "t.test", ref.group = "0")
Gives the following plot, that only takes into account the highest value in facet 1.
I would like to place the asterisk on the faceted plot 0 at the top of the box, like it is in the plot 1. In this case I can extract the maximum value of each boxplot by:
lab_coords = mtcars %>% group_by(am,vs) %>%
summarize(max_mpg = max(mpg)) %>% pull(max_mpg)
lab_coords
[1] 19.2 24.4 26.0 33.9
When I pass the label coordinates to stat_compare_means I cannot place them in the correct order:
ggboxplot(mtcars, x = "am", y = "mpg") +
facet_wrap(~vs) +
stat_compare_means(aes(label = ..p.signif..),
method = "t.test", ref.group = "0",
label.y = lab_coords)
Is there any way of passing different ylabel positions to each facet?