R 在ggplot2方框图中的组内和组之间添加显著性条

R 在ggplot2方框图中的组内和组之间添加显著性条,r,ggplot2,ggpubr,R,Ggplot2,Ggpubr,如果我有以下按类型和子类型组织的数据帧: df = rbind( data.frame(type="A", subtype="c", value=rnorm(mean=1, 100)), data.frame(type="B", subtype="c", value=rnorm(mean=1.5, 100)), data.frame(type="A", subtype="d&

如果我有以下按类型和子类型组织的数据帧:

df = rbind(
  data.frame(type="A", subtype="c", value=rnorm(mean=1, 100)),
  data.frame(type="B", subtype="c", value=rnorm(mean=1.5, 100)),
  data.frame(type="A", subtype="d", value=rnorm(mean=2, 100)),
  data.frame(type="B", subtype="d", value=rnorm(mean=2.5, 100))
)
我创建了一个方框图,如下所示:

p = ggplot(df, aes(x=type, y=value, color=subtype)) +
  geom_boxplot(outlier.shape = NA)

我知道我可以通过以下操作在每种类型的子类型之间(即A.c和A.d之间,以及B.c和B.d之间)添加重要性条:

p + ggpubr::stat_compare_means(test = 'wilcox.test', label = 'p.signif', show.legend = F)
如何在选定列之间添加重要性栏?例如,如果我想在a.c和B.c之间添加一个重要性条,我该怎么做? 有什么方法可以调用这些列吗?

ggpubr::stat\u compare\u函数的
comparations
参数意味着
函数显然包含列索引,但我不知道如何引用子类型索引

我想要的东西如下所示:

p + ggpubr::stat_compare_means(test = 'wilcox.test', label = 'p.signif', show.legend = F)

以下是一个基于以下因素的潜在解决方案:

库(tidyverse)
图书馆(GGF)
df=rbind(
数据帧(type=“A”,subtype=“c”,value=rnorm(mean=1100)),
数据帧(type=“B”,subtype=“c”,value=rnorm(mean=1.5100)),
数据帧(type=“A”,subtype=“d”,value=rnorm(mean=2100)),
数据帧(type=“B”,subtype=“d”,value=rnorm(平均值=2.5100))
)

df$interaction实际上我自己也找到了一个类似的解决方案,但理想情况下,我希望将X轴作为类型变量,因为分组对我的数据很重要。如果可以在保持X轴按类型分组的同时添加重要性条,我愿意这样做,否则您的答案可能是最好的选择,我将接受答案。我无法按类型分组X轴抱歉;希望其他人有一个聪明的解决方案。这个答案()还涉及到一个潜在的解决方法(计算p值并手动绘制),谢谢!最后我把它作为一种解决方法,但当子类型或类型由两个以上的唯一值组成时,我无法找到一种智能地添加重要性条的方法。
library(tidyverse)
library(ggsignif)
library(ggbeeswarm)

df = rbind(
  data.frame(type="A", subtype="c", value=rnorm(mean=1, 100)),
  data.frame(type="B", subtype="c", value=rnorm(mean=1.5, 100)),
  data.frame(type="A", subtype="d", value=rnorm(mean=2, 100)),
  data.frame(type="B", subtype="d", value=rnorm(mean=2.5, 100))
)

df$Category <- factor(interaction(df$type, df$subtype),
                      levels = c("A.c", "A.d", "B.c", "B.d"),
                      labels = c("Type A\nSubtype c", "Type A\nSubtype d",
                                 "Type B\nSubtype c", "Type B\nSubtype d"))

ggplot(df, aes(x=Category, y=value)) +
  geom_boxplot(aes(colour = subtype),
               outlier.shape = NA) +
  geom_signif(comparisons = list(c("Type A\nSubtype c", "Type B\nSubtype c"),
                                 c("Type A\nSubtype c", "Type A\nSubtype d"),
                                 c("Type B\nSubtype c", "Type B\nSubtype d"),
                                 c("Type A\nSubtype d", "Type B\nSubtype d")),
              test = "wilcox.test", step_increase = 0.075,
              map_signif_level = TRUE, tip_length = 0) +
  geom_quasirandom(aes(fill = type), shape = 21, 
                groupOnX = TRUE, size = 2, alpha = 0.5) +
  scale_fill_viridis_d(option = "D", begin = 0, end = 0.3)