Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with 'stat_compare_means' when using the 'facet_wrap' function

I am trying to create a faceted plot like below. However, stats always only get applied to one of the plots. Is there a workaround keeping the figure appearance the same, but adding stats to both?

Here is the code that I am using

ggplot(dat, aes(x=`Treatment`,y=`Endostatin`, fill=Treatment))+

geom_boxplot(outlier.alpha = 0.25, outlier.color = "red") +

geom_point(alpha = 0.25, size = 2 ) +

facet_wrap(~Mode, scale="free") +

stat_compare_means(comparisons = my_comparisons) +

theme(axis.text.x = element_text(angle = 20))

Output plot_edited

like image 611
Gedara Home Avatar asked Jan 22 '26 11:01

Gedara Home


1 Answers

You should add stat_compare_means separately

my_comparisons1 <- list(c("SHAM+vehicle", "TAC+vehicle"), c("TAC+vehicle", "TAC+relaxin"), c("TAC_vehicle", "TAC+Enalapril"))
my_comparisons2 <- list(c("TAC+vehicle", "TAC+relaxin")

stat_compare_means(comparisons = my_comparisons1 ) +
  stat_compare_means(comparisons = my_comparisons2 )
like image 121
Park Avatar answered Jan 24 '26 06:01

Park