R 箱线图,使用 "stat_compare_means" 更改 p 值中的位数
R boxplot, change number of digits in p value using "stat_compare_means"
使用 ToothGrowth 数据集(内置于 R 中),我使用了以下代码。
library(ggplot2)
library(tidyverse)
library(ggpubr)
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
color = "supp", palette = "jco",
add = "jitter",
facet.by = "dose", short.panel.labs = FALSE)
p + stat_compare_means(label = "p.format")
现在,我希望 p 值有 4 位数。我研究了以前的类似帖子,然后尝试了以下两个选项
p + stat_compare_means(label = "p.format", digits = 4)
p + stat_compare_means(label = "p.format", round(p.format, 4))
不幸的是,两者都没有用。有人可能有解决方案吗?谢谢。
这是一个使用 sprintf
的选项
library(ggplot2)
library(tidyverse)
library(ggpubr)
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
color = "supp", palette = "jco",
add = "jitter",
facet.by = "dose", short.panel.labs = FALSE)
p + stat_compare_means(aes(label = sprintf("p = %5.4f", as.numeric(..p.format..))))
更新
为了回应您的评论,您可以执行以下操作
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
color = "supp", palette = "jco",
add = "jitter",
facet.by = "dose", short.panel.labs = FALSE)
p + stat_compare_means(aes(label = ifelse(
p < 1.e-2,
sprintf("p = %2.1e", as.numeric(..p.format..)),
sprintf("p = %5.4f", as.numeric(..p.format..)))))
这里我使用科学计数法打印 p-value if p < 1.e-2
else 作为 4 位浮点数。
使用 ToothGrowth 数据集(内置于 R 中),我使用了以下代码。
library(ggplot2)
library(tidyverse)
library(ggpubr)
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
color = "supp", palette = "jco",
add = "jitter",
facet.by = "dose", short.panel.labs = FALSE)
p + stat_compare_means(label = "p.format")
现在,我希望 p 值有 4 位数。我研究了以前的类似帖子,然后尝试了以下两个选项
p + stat_compare_means(label = "p.format", digits = 4)
p + stat_compare_means(label = "p.format", round(p.format, 4))
不幸的是,两者都没有用。有人可能有解决方案吗?谢谢。
这是一个使用 sprintf
library(ggplot2)
library(tidyverse)
library(ggpubr)
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
color = "supp", palette = "jco",
add = "jitter",
facet.by = "dose", short.panel.labs = FALSE)
p + stat_compare_means(aes(label = sprintf("p = %5.4f", as.numeric(..p.format..))))
更新
为了回应您的评论,您可以执行以下操作
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
color = "supp", palette = "jco",
add = "jitter",
facet.by = "dose", short.panel.labs = FALSE)
p + stat_compare_means(aes(label = ifelse(
p < 1.e-2,
sprintf("p = %2.1e", as.numeric(..p.format..)),
sprintf("p = %5.4f", as.numeric(..p.format..)))))
这里我使用科学计数法打印 p-value if p < 1.e-2
else 作为 4 位浮点数。