问题
I'm trying to add a label on boxplot using ggpubr package. Here's the code I used:
library(ggplot2)
library(ggpubr)
compare_means(len ~ supp, data = ToothGrowth, method="t.test", paired=TRUE, group.by = "dose")
# Box plot facetted by "dose"
p1 <- ggboxplot(ToothGrowth, x = "supp", y = "len", xlab=F,
color = "supp", palette = "jco",
facet.by = "dose", add="mean", short.panel.labs = FALSE)
# Use only p as label.
p2 <- p1 + stat_compare_means(method = "t.test", paired = T, label = "p")
p2
And the sample data (ToothGrowth)
15.5 VC 1.0
23.6 VC 2.0
18.5 VC 2.0
33.9 VC 2.0
25.5 VC 2.0
26.4 VC 2.0
32.5 VC 2.0
26.7 VC 2.0
21.5 VC 2.0
23.3 VC 2.0
29.5 VC 2.0
15.2 OJ 0.5
21.5 OJ 0.5
17.6 OJ 0.5
9.7 OJ 0.5
14.5 OJ 0.5
10.0 OJ 0.5
8.2 OJ 0.5
9.4 OJ 0.5
16.5 OJ 0.5
9.7 OJ 0.5
Is there any way to add a mean value as a text (other than the dot using : add="mean")
If possible, I'd like to place the mean value text right under the "p=0.000" which comes from the "stat_compare_means" code.
also, is there a way to remove the legend automatically generated when grouping the plot?
the final plot I want to make looks like this :
- add the mean value as text on the boxplot
- remove the legend at the top
Thanks in advance!
回答1:
You can try
p1 + stat_compare_means(method = "t.test", paired = T, label = "p", label.x = 0.8)+
stat_summary(fun.data = function(x) data.frame(y=32, label = paste("Mean=",mean(x))), geom="text") +
theme(legend.position="none")
来源:https://stackoverflow.com/questions/50202895/add-mean-value-on-boxplot-with-ggpubr