ggcorrplot icon indicating copy to clipboard operation
ggcorrplot copied to clipboard

feature request: displaying significance asterisks with coefficient labels

Open IndrajeetPatil opened this issue 6 years ago • 4 comments

Will it be possible for ggcorrplot to allow users to not only display the correlation coefficients but also asterisks denoting the significance level?

Like: http://www.strengejacke.de/sjPlot/reference/sjp.corr.html#examples

image

IndrajeetPatil avatar Nov 18 '19 09:11 IndrajeetPatil

good suggestion, thanks!

kassambara avatar Nov 18 '19 17:11 kassambara

You can try my R package ggcorrplot2, which can label significant correlation coefficients with asterisks denoting the significance level.

image

caijun avatar Dec 14 '19 08:12 caijun

@caijun Cool! You should submit this to https://github.com/ggplot2-exts/gallery to increase its visibility!

IndrajeetPatil avatar Dec 14 '19 12:12 IndrajeetPatil

Hi, I know it's been more than a year but I also wanted to achieve this functionality using ggcorrplot and I played around with the source code to obtain a solution.

If you copy the whole code for the ggcorrplot() function, then on line 215 the original code is: p.mat <- subset(p.mat, p.mat$value > sig.level) Substitute this with p.mat <- subset(p.mat, p.mat$value <= sig.level & p.mat$value != 0) The != 0 part is assuming you would choose the full correlation plot and is there to prevent the diagonal in the middle from having the asterisk and I believe no p value should practically be 0 so it should be fine

Then on line 295 I added the position argument inside the function:

# matrix cell glyphs
  if (!is.null(p.mat) & insig == "pch") {
    p <- p + ggplot2::geom_point(
      data = p.mat,
      mapping = ggplot2::aes_string(x = "Var1", y = "Var2"),
      shape = pch,
      size = pch.cex,
      color = pch.col,
      position = position_nudge(x=0.35,y=0.16)
    )
  }

Of course, depending on visual preference, the nudge can be customized through the x and y argument. Then in the function, just choose pch = 8 (this is the asterisk) and pch.cex = 1 (size of asterisk) and that should basically achieve the objective of showing asterisk for significant correlations instead of showing crosses for insignificant correlations. Hopefully it can be of use in the future!

Jeff87075 avatar May 22 '21 16:05 Jeff87075