0

I am looking for help to change the presentation of the p-value on the display in a boxplot.

For now: var = different samples (blood etc.) group: sample.group (positive / negative)..

When running the following code, I get a boxplot with a p-value. The p-value is 6.7e^-07, what I need help doing is:

  1. Writing p-value with only 3 digits.
  2. If p<0.001, I want it to write <0.001 on the boxplot.

Can anyone help?

My code is:

library(ggpubr)   

fig_boxplot <- function(df, var, group, title = " "){
  
  temp <- aggregate(df[[group]][!is.na(df[[var]])],
                    by = list(df[[group]][!is.na(df[[var]])]), length)
  colnames(temp) <- c("sample.group","n")
  temp$new_name <- paste0(temp$sample.group, "\n(n = ", temp$n, ")")
  df <- merge(df,
              temp[,c("sample.group","new_name")],
              by = "sample.group", all.x=F)
  my_comparisons <- list( unique(df$new_name))
  
  g1 <- ggplot(data=df, aes(x=df$new_name, y=df[[var]], color=df$new_name)) + 
    geom_boxplot() + 
    geom_jitter(width=0.2) +
    theme_minimal() + 
    theme(axis.ticks.x=element_blank(),
          axis.text.y = element_text(angle=90, hjust=0.5)) +
    labs(title=title) +
    stat_compare_means(comparisons = my_comparisons,label = "p.adj",hjust=0) + 
    theme(plot.subtitle = element_text(hjust=0.5, face="bold")) +
    scale_color_manual(values=c("#666666", "#000000")) +
    theme(axis.title = element_blank(), 
          axis.text.y = element_text(angle=0)) +
    theme(legend.position = "none") + 
    coord_flip() + 
    theme(plot.margin=unit(c(0,0,0,0), "mm"))
  
  return(g1)
}

fig_boxplot(df, var = "CSF.WBC.RBC.ratio", group, title = "A) CSF-WBC / CSF-RBC")
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 17 '23 at 13:03

2 Answers2

0

You could use the geom_signif() function from the ggsignif package. It allows you to select the groups you want to compare and to map significance with *, ** and ***.

0

Converting a comment from teunbrand to an answer:

You can try scales::pvalue(c(0.5, 0.1, 1e-5), accuracy = 0.001) for example.

TylerH
  • 20,799
  • 66
  • 75
  • 101