用ggpubr在箱线图上添加平均值 [英] add mean value on boxplot with ggpubr

查看:623
本文介绍了用ggpubr在箱线图上添加平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggpubr软件包在boxplot上添加标签.这是我使用的代码:

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

和样本数据(ToothGrowth)

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

是否有任何方法可以将平均值添加为文本(除了点号之外,使用:add ="mean")

Is there any way to add a mean value as a text (other than the dot using : add="mean")

如果可能的话,我想将平均值文本放在来自"stat_compare_means"代码的"p = 0.000"下.

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 :

  1. 将平均值作为文本添加到箱线图中
  2. 删除顶部的图例

提前谢谢!

推荐答案

您可以尝试

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")

这篇关于用ggpubr在箱线图上添加平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆