Home > Enterprise >  Transform Stat compare means function to p values less or greater that 0.05 in ggplot in R
Transform Stat compare means function to p values less or greater that 0.05 in ggplot in R

Time:04-23

How to transform stat_compare_means function, so that it shows on the graph p-value > or < 0.05, not the number or asterisks?

Figure

Currently it shows only number with digits not the < or > 0.05

gg.plot <- ggplot(df, aes(x=Group, y=Lev, alpha==Group, fill=Group))  
  geom_boxplot()  
  scale_fill_manual(values=c('#D32F2F','#3F51B5'))

gg.plot <- gg.plot   stat_compare_means(aes(label = format.pval(..p.adj.., digits = 3), label =  "p.signif", label.x = 1.5, label.y = 49)

CodePudding user response:

You can define your own labels using the symnum.args argument within stat_compare_means. Set your cutpoints and add symbols or text or whatever you would like to have.

Some reproducible data:

set.seed(1)
Group <- c(rep("A", 50), rep("B", 50), rep("C", 50))
Lev = c(rnorm(50, 5, 2), rnorm(50, 7, 3), rnorm(50, 7, 2))
df <- data.frame(Group, Lev)  

ggplot(df, aes(x=Group, y=Lev, fill=Group))  
  geom_boxplot()  
  stat_compare_means(comparisons = list(c("A", "B"), c("B", "C")),
                     method = "t.test",
                     symnum.args = list(cutpoints = c(0, 0.05, Inf), 
                                        symbols = c("p <0.05", "p >0.05")))

enter image description here

  • Related