将均值比较添加到绘图 + 是否可以在 KS 测试中显示 ggplot(或一般的 R)中的 p 值,特别是在小提琴图上?

Adding mean comparisons to plot + Is it possible to display p-values in ggplot (or R in general) from a KS test, specifically on a violin plot?

所以我正在寻求创建这样的东西:

Example Output

使用我自己的数据,特别是使用我在此处找到的 p 值:

KS test p-values

现在,我能够制作出类似的东西,尽管方法不正确。具体来说,我能够使用 T 检验生成类似的东西: T test p-value

我通过编写这段代码生成了这个:

l<- ggplot(VioPos, aes(x=Regulation, y=Score,fill=Regulation)) +
  geom_violin(trim=FALSE)+
  labs(title="Plot of ARE Scores by Regulation",x="Gene Regulation", y = "ARE Score")+
  geom_boxplot(width=0.1,fill="white")+
  theme_classic()
l

dp <- l +  scale_y_continuous(trans="log2")
dp



dp7 <- dp +
  stat_compare_means(comparisons=my_comparisons, method="t.test")
dp7

换句话说,我使用 stat_compare_means() 使用 ggplot2/tidyverse/ggpubr/rstatix。

但是,如果我修改代码中的方法,它似乎可以正确显示 Wilcoxon 和 T 检验,但不能正确显示 anova 和 kruskal wallis 检验。此外,stat_compare_means() 似乎只支持这四个而不支持 KS,但我特别感兴趣的是将我的 KS 测试输出的平均比较绘制到我的小提琴图上。 我可以使用其他软件包吗?

另请注意:对于 KS 测试,“UpScorePos”“DownScorePos”等是根据规则比较 ARE 分数(就像我在 T 测试中对图表所做的那样)。

您可以像这样从 KS 检验中获取 p 值:

x <- rnorm(100)
y <- rnorm(100)
res <- ks.test(x, y)
res$p.value
[1] 0.9670685

只需使用此 p 值并将其添加到您的绘图中即可。

编辑:一个有点棘手的解决方案是使用 运行 t 检验并获得可与 stat_pvalalue_manual 一起使用的正确数据结构,并从 ks.test 中插入 pvalues .请参阅下面的示例(我以 ToothGrowth 数据为例)。

# Transform `dose` into factor variable
df <- ToothGrowth
df$dose <- as.factor(df$dose)

stat.test <- df %>%
  t_test(len ~ dose)
stat.test

# prepare test tibble for ks.test
stat.test <- df %>%
  t_test(len ~ dose)
stat.test <- stat.test %>% add_y_position()
stat.test

kst <- stat.test # copy tibble to overwrite p-values for ks.test

p1 <- ks.test(x = ToothGrowth$len[ToothGrowth$dose == 0.5],
              y = ToothGrowth$len[ToothGrowth$dose == 1]
)$p
p2 <- ks.test(x = ToothGrowth$len[ToothGrowth$dose == 0.5],
              y = ToothGrowth$len[ToothGrowth$dose == 2]
)$p
p3 <- ks.test(x = ToothGrowth$len[ToothGrowth$dose == 1],
              y = ToothGrowth$len[ToothGrowth$dose == 2]
)$p

kst[, 'p'] <- as.numeric(c(p1, p2, p3))

ggplot(df, aes(x = dose, y = len)) +
  geom_violin(trim = F) +
  stat_pvalue_manual(kst, label = "p = {p}")