我嘗試制作帶有顯示 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")
結果是

但是如何向計算出的 p 值添加額外的文本?我要添加“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", 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..)))
uj5u.com熱心網友回復:
我相信這可能是 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()

由
為了更好地控制格式和舍入,您可以使用它或更改為您希望的任何格式。
mutate(myformatted.p = paste0("p = ", ifelse(p < 1e-5, format(p, scientific = T), signif(p, digits = 2))))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376435.html
下一篇:R:將兩個等值線圖組合在一起
