gpt4 book ai didi

r - 将自定义注释添加到 p 值标签到 ggpubr stat_compare_means()

转载 作者:行者123 更新时间:2023-12-05 03:33:54 54 4
gpt4 key购买 nike

我尝试制作显示 p 值的箱线图

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len")+
stat_compare_means(comparisons = my_comparisons, method = "wilcox.test")

结果是

enter image description here

但是如何向计算的 p 值添加额外的文本?我想添加 "p = " 如下所示

enter image description here

我怎样才能做到?

upd. 下面的变体不起作用

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )

ggboxplot(ToothGrowth, x = "dose", y = "len")+
stat_compare_means(comparisons = my_comparisons, method = "wilcox.test", aes(label=paste("p=",scientific(as.numeric(..p.format..)))))

ggboxplot(ToothGrowth, x = "dose", y = "len")+
stat_compare_means(comparisons = my_comparisons, method = "wilcox.test", aes(label = paste("p =", ..p.format..)))

ggboxplot(ToothGrowth, x = "dose", y = "len")+
stat_compare_means(comparisons = my_comparisons, method = "wilcox.test", aes(label = paste0("p =", ..p.format..)))

最佳答案

我相信这可能是 ggpubr 中的潜在错误。该文档提供了一个示例,说明如何将自定义文本添加到您的 p 值。当将多个比较传递给标记函数时,这开始失败。我认为该错误可能是 ggplot 不知道将哪个表达式解析为标签(请参阅下面的 r-lang 警告)。还有一些关于可能相关的 p 值计算的警告。

也就是说,我觉得大多数关于 ggpubr 的问题都与具有多个 p 值的注释有关,我确实觉得您可能需要重新考虑 (a) 您的统计数据和 (b) 您的可视化。

library(ggpubr)
#> Loading required package: ggplot2
library(tidyverse)

## This works as expected. Only one test
ggboxplot(ToothGrowth, x = "supp", y = "len") +
stat_compare_means(aes(label = paste0("p = ", ..p.format..)))

看到大量警告 - 开始失败。

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len") +
stat_compare_means(comparisons = my_comparisons,
aes(label = paste0("p = ", ..p.format..)))
#> 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.
#> Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
#> cannot compute exact p-value with ties
#> Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
#> cannot compute exact p-value with ties
#> Warning in wilcox.test.default(c(16.5, 16.5, 15.2, 17.3, 22.5, 17.3, 13.6, :
#> cannot compute exact p-value with ties

通过分别为每个比较组创建一个图,这又开始起作用了。

ls_tg <- ToothGrowth %>%
split(., .$dose)

lapply(my_comparisons, function(x) bind_rows(ls_tg[x])) %>%
map(~ggboxplot(., x = "dose", y = "len") +
stat_compare_means(aes(label = paste0("p = ", ..p.format..)))) %>%
patchwork::wrap_plots()

reprex package 创建于 2021-12-07 (v2.0.1)

关于r - 将自定义注释添加到 p 值标签到 ggpubr stat_compare_means(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70260121/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com