您好,我有一个示例 df 如下,我正在尝试使用ggpubr带有该stat_compare_means()函数的包执行 t 测试,但是我无法让它在我想要的组之间进行比较:
set.seed(13)
df = data.frame(grouping = c(rep("A",24),rep("B",24),rep("C",24)),
id = c(rep(1,12),rep(2,12),rep(3,12),rep(4,12),rep(5,12),rep(6,12)),
subject = c(rep(c("One","Two","Three","Four"),72)),
family = c(rep(c("Red","Red","Red","Red","Blue","Blue","Blue","Blue","Green","Green","Green","Green"),6)),
percent = sample(1:100,288,replace=T),
result = "Good")
df %>%
ggplot(aes(x=subject,y=percent,group = interaction(subject,grouping))) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(color = grouping),size=1.75,position = position_dodge(width = 0.75))+
scale_y_continuous(breaks = seq(0, 100, by = 25),limits = c(0,103)) +
facet_grid(result~family,switch = "y") +
scale_x_discrete("") +
theme_minimal() +
stat_compare_means(aes(group = grouping),label="p.signif",
method = "t.test",paired=F,
tip.length=0,step.increase = 0.05)
Run Code Online (Sandbox Code Playgroud)
从图中可以看出,它只有一个“ns”用于统计测试,但是否有可能在所有三个比较中都使用它?我希望测试在 A、B 和 C 的分组变量之间进行,例如在“四”的 3 个箱线图中,将有 3 个 A 与 B、A 与 C 和 B 与 C 的比较。我也尝试添加comparisons = list(c("A","B"),c("A","C"),c("B","C"))作为comparisons参数,但它也不起作用。多谢!
编辑
这里的关键是比较基于:
比较:长度为 2 的向量列表。向量中的条目要么是 x 轴上 2 个值的名称,要么是与要比较的感兴趣组的索引相对应的 2 个整数。
因此,您必须interaction在 x 轴上使用才能比较每个分组的箱线图。这是一个可重现的示例:
library(ggplot2)
library(ggpubr)
library(dplyr)
df %>%
ggplot(aes(x=interaction(subject, grouping),y=percent)) +
geom_boxplot(aes(group = interaction(subject, grouping)), outlier.shape = NA) +
geom_point(aes(color = grouping, group = grouping),size=1.75,position = position_dodge(width = 0.75))+
scale_y_continuous(breaks = seq(0, 100, by = 25)) +
scale_x_discrete("", limits = c("Four.A", "Four.B", "Four.C", "One.A", "One.B", "One.C", "Three.A", "Three.B", "Three.C", "Two.A", "Two.B", "Two.C"),
labels = c(rep(c("", "Four", "", "", "One", "", "", "Three", "", "", "Two", ""), 3))) +
stat_compare_means(comparisons = list(c("Four.A","Four.B"),c("Four.A","Four.C"),c("Four.B","Four.C"),
c("One.A","One.B"),c("One.A","One.C"),c("One.B","One.C"),
c("Three.A","Three.B"),c("Three.A","Three.C"),c("Three.B","Three.C"),
c("Two.A","Two.B"),c("Two.A","Two.C"),c("Two.B","Two.C")),
method = "t.test",
paired=F,
tip.length=0,step.increase = 0.05) +
facet_wrap(~ family) +
theme_minimal()
Run Code Online (Sandbox Code Playgroud)

创建于 2022-08-31,使用reprex v2.0.2
旧答案
也许你想要这样的东西。另请确保删除limits中的scale_y_continuous,否则标签将不会显示。这是一个可重现的示例:
set.seed(13)
df = data.frame(grouping = c(rep("A",24),rep("B",24),rep("C",24)),
id = c(rep(1,12),rep(2,12),rep(3,12),rep(4,12),rep(5,12),rep(6,12)),
subject = c(rep(c("One","Two","Three","Four"),72)),
family = c(rep(c("Red","Red","Red","Red","Blue","Blue","Blue","Blue","Green","Green","Green","Green"),6)),
percent = sample(1:100,288,replace=T),
result = "Good")
library(ggplot2)
library(ggpubr)
library(dplyr)
df %>%
ggplot(aes(x=subject,y=percent, group = interaction(subject,grouping))) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(color = grouping, group = grouping),size=1.75,position = position_dodge(width = 0.75))+
scale_y_continuous(breaks = seq(0, 100, by = 25)) +
facet_wrap(~ family) +
scale_x_discrete("") +
theme_minimal() +
stat_compare_means(comparisons = list(c("Four", "One"), c("Four", "Three"), c("Four", "Two"), c("One", "Three"), c("One", "Two"), c("Three", "Two")),
aes(label=..p.adj..),
method = "t.test",paired=F,
tip.length=0,step.increase = 0.05)
#> Warning: Using `as.character()` on a quosure is deprecated as of rlang 0.3.0.
#> Please use `as_label()` or `as_name()` instead.
#> This warning is displayed once per session.
Run Code Online (Sandbox Code Playgroud)

创建于 2022-08-28,使用reprex v2.0.2
| 归档时间: |
|
| 查看次数: |
1508 次 |
| 最近记录: |