该函数stat_pvalue_manual()将使用 向箱线图添加 p 值ggboxplot。然而,打印的 p 值有时有很多位长。我想将小数位数限制为 3 位。我该怎么做?
从下图中您将看到 versicolor 和 virginica 的 p 值为小数点后 5 位,如何调整下面的代码以报告小数点后 3 位(即 0.009)?
library(tidyverse)
library(rstatix)
library(ggpubr)
test <- iris
test$Species <- as.factor(test$Species)
test.aov <- test %>% anova_test(Sepal.Width ~ Species)
test.tukey <- test %>% tukey_hsd(Sepal.Width ~ Species)
test.tukey <- test.tukey %>% add_xy_position(x = "Species")
ggboxplot(test, x = "Species", y = "Sepal.Width", outlier.shape = NA) +
stat_pvalue_manual(test.tukey, hide.ns = TRUE, y.position = c(5,4.5,4), label = "p = {p.adj}") +
geom_jitter(shape=16, alpha = 0.4, size = 2, position=position_jitter(0.1)) +
labs(subtitle = get_test_label(test.aov, detailed = T)) +
scale_y_continuous(breaks = seq(0,5,1), limits = c(0,5)) +
xlab("Species") +
ylab("Sepal Length") +
theme_bw() +
theme(panel.grid = element_blank(),
plot.subtitle = element_text(vjust = -105, hjust = 0.05),
text = element_text(size = 14),
axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"))
Run Code Online (Sandbox Code Playgroud)
你可以使用label = "p = {scales::pvalue(p.adj)}"
ggboxplot(test, x = "Species", y = "Sepal.Width", outlier.shape = NA) +
stat_pvalue_manual(test.tukey, hide.ns = TRUE, y.position = c(5,4.5,4),
label = "p = {scales::pvalue(p.adj)}") +
geom_jitter(shape=16, alpha = 0.4, size = 2, position=position_jitter(0.1)) +
labs(subtitle = get_test_label(test.aov, detailed = T)) +
scale_y_continuous(breaks = seq(0,5,1), limits = c(0,5)) +
xlab("Species") +
ylab("Sepal Length") +
theme_bw() +
theme(panel.grid = element_blank(),
plot.subtitle = element_text(vjust = -105, hjust = 0.05),
text = element_text(size = 14),
axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"))
Run Code Online (Sandbox Code Playgroud)