Home > OS >  In R, how can I adjust the number of significant digits in stat_pvalue_manual()?
In R, how can I adjust the number of significant digits in stat_pvalue_manual()?

Time:03-14

The function stat_pvalue_manual() will add p-values to boxplots using ggboxplot. However, the p-values that are printed are sometimes many digits long. I would like to limit the number of decimal places to 3. How can I do that?

From the plot below you will see that the p-value for versicolor and virginica is 5 decimal places, how can I adjust the code below to report back 3 decimal places (i.e., 0.009)?

library(ggplot2)
library(tidyverse)
library(dplyr)
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"))

CodePudding user response:

You could uselabel = "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"))

enter image description here

  • Related