3
votes

I would like to show the significance levels (*** or n.s.) as labels in my linear regression using ggpubr in R. This seems to be done by using aes(label = ..p.signif..) as posted here: https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/

However, when I simply replace the ..p.label.. by ..p.signif.. in my stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~,~")) ie. stat_cor(aes(label = paste(..rr.label.., ..p.signif.., sep = "~,~"))` nothing on my plot change, just I get an error:

Error in paste(rr.label, p.signif, sep = "~`,`~") : 
  object 'p.signif' not found 

Please, how can I plot the stars (*, , *) or n.s. values instead of exact p-values on my plot? THank you very much.

My dummy data: (borrowed from http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/78-perfect-scatter-plots-with-correlation-and-marginal-histograms/)


library(ggpubr)
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)

ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line",                         # Add regression line
          conf.int = TRUE,                          # Add confidence interval
          color = "cyl", palette = "jco",           # Color by groups "cyl"
          shape = "cyl"                             # Change point shape by groups "cyl"
)+
  stat_cor(aes(color = cyl,
               label =paste(..rr.label.., ..p.label.., sep = "~`,`~")), # HOW TO CHANGE p.label to show stars???
           label.x = 3)           # Add correlation coefficient

enter image description here

1

1 Answers

3
votes

You can use cut:

ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line",                         # Add regression line
          conf.int = TRUE,                          # Add confidence interval
          color = "cyl", palette = "jco",           # Color by groups "cyl"
          shape = "cyl"                             # Change point shape by groups "cyl"
)+
  stat_cor(aes(color = cyl,
               label =paste(..rr.label.., cut(..p.., 
                                              breaks = c(-Inf, 0.0001, 0.001, 0.01, 0.05, Inf),
                                              labels = c("'****'", "'***'", "'**'", "'*'", "'ns'")), 
                            sep = "~")), 
           label.x = 3)   

resulting plot showing significance stars

Needless to say that showing the p-values (or, even better, the confidence intervals) is much better.