1

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

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)

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

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

另外,有没有办法删除分组情节时自动生成的图例?

我想要制作的最终情节如下所示:

https://i.stack.imgur.com/qudMy.png

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

提前致谢!

4

1 回答 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")

在此处输入图像描述

于 2018-05-09T08:08:39.483 回答