I am having trouble adding the p values for the three comparisons in the grouped boxplots (Acini-cr_POPF, Fibrosis-cr_POPF, Fat-cr_POPF). I tried using stat_compare_means, stat_p_value, nothing works. Then, i used facet.by in order to procude the boxplots attached, and managed to overcome the problem of the previous syntax. I could use some help with the p values: i need the code for a loop function, that will do the following: if p < 0.001 , print "p < 0.001" else print p value with 3 decimal points. Any ideas how i can intergrate the loop function into stat_compare_means? Or is there another way to do it..?? Thanks in advance!
First, i tried the following: ###create dataset:
Boxplot_dataset <- Data_final%>%
select(cr_POPF,Acini,Fibrosis,Fat)%>%
mutate(cr_POPF=factor(cr_POPF,levels=c(0,1),labels=c("No","Yes")))
use melt to combine the data:
dat.m <- melt(Boxplot_dataset,id.vars='cr_POPF', measure.vars=c('Acini','Fibrosis','Fat'))
then used stat_compare_means as follows (which produced the first boxplot attached)Boxplot_stat_compare_means:
p2 <- ggplot(dat.m) +
geom_boxplot(aes(x= cr_POPF, y=value, fill=variable))+
stat_compare_means(
(aes(x=cr_POPF,y=value,group=variable)),
method="anova",label.y = c(29, 35, 40),
label = "p.signif",
)
###since i could not compare the histology according to cr_POPF (e.g. Acini-cr_POPF, Fibrosis_cr_POPF, etc), then i tried facet.by: `bxp <- ggboxplot( dat.m, x = "cr_POPF", y = "value",fill="variable", facet.by = "variable")
bxp + stat_compare_means( aes(label = paste0("p = ", after_stat(p.format))) )`
Boxplot_facet.bythis works fine for me (since there seems to be no way to add the p values for pairwise comparisons to the first boxplot), i now need the p values to appear in another form. Maybe using a loop function..?? Is this possible with stat_compare_means??