Search code examples
rggplot2ggpubr

Is there a way to change the asterisks to match custom p-values?


For my paper, I need to format the pvalues to pvalue< 0.1 already showing as a significant value (*). The other values should stay the same (so ** < 0.01 and so on).

I used beforehand this part of an ggplot:

stat_compare_means(aes(label = paste0('', after_stat(p.format), ' ',
after_stat(p.signif))), label.y = 450,
vjust = 1, method = "t.test", ref.group = "0", size=3,fontface="bold")

it combines the pvalue and the asterisks. Of course it shows the asterisks for the "normal" pvalues (ie. *< 0.05, ns > 0.05).


Solution

  • If you look at ?stat_compare_means, you will find the description of the argument symnum.args:

    symnum.args
    a list of arguments to pass to the function symnum for symbolic number coding of p-values. For example, symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("****", "***", "**", "*", "ns")).

    Without a reproducible example, it's difficult to see how an answer here could add much to this, but I suppose we can generate a fake data set that approximates yours:

    library(ggpubr)
    
    set.seed(4)
    
    df <- data.frame(xvals = rep(0:4, each = 100), 
                     yvals = rnorm(500, rep(71:75 * 5, each = 100), 25))
    
    ggboxplot(df, x = "xvals", y = "yvals",
              color = "xvals", palette = "npg") +
      stat_compare_means(aes(label = paste0('', after_stat(p.format), ' ',
                                            after_stat(p.signif))), 
            label.y = 450, vjust = 1, method = "t.test",
            ref.group = "0", size = 3, fontface = "bold",
            symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01,  0.1, 1), 
                               symbols = c("****", "***", "**", "*", "ns")))
    

    Created on 2023-07-24 with reprex v2.0.2