rcorrelationsignificance

R corrplot: Plot correlation coefficients along with significance stars?


Using R corrplot, I have not found a solution where the correlation coefficients in the boxes are plotted together with their significances, i.e. 0.84*** Here is the code plotting only the significance stars. How can the correlation coefficients be added there?

M<-cor(mtcars)
res1 <- cor.mtest(mtcars, conf.level = .95)
corrplot(cor(mtcars),
     method="square",
     type="lower",
     p.mat = res1$p,
     insig = "label_sig",
     sig.level = c(.001, .01, .05),
     pch.cex = 0.8,
     pch.col = "red",
     tl.col="black",
     tl.cex=1,
     outline=TRUE)

If I add, as by the first answer suggested, addCoef.col = "black", the text overlays the significance stars so they cannot really be seen anymore: enter image description here


Solution

  • The position of the significance stars is defined by the place_points function within the corrplot function.

    Problem:

    If both, the correlation coefficients and the significance level should be displayed, they overlap (I took yellow for the stars since I have some issues with colour vision...).

    library(corrplot)
    #> corrplot 0.90 loaded
    
    M<-cor(mtcars)
    res1 <- cor.mtest(mtcars, conf.level = .95)
    
    corrplot(cor(mtcars),
             method="square",
             type="lower",
             p.mat = res1$p,
             insig = "label_sig",
             sig.level = c(.001, .01, .05),
             pch.cex = 0.8,
             pch.col = "yellow",
             tl.col="black",
             tl.cex=1,
             addCoef.col = "black",
             tl.pos="n",
             outline=TRUE)
    

    Created on 2021-10-13 by the reprex package (v2.0.1)

    Quick and temporary (you have to re-do this step everytime you newly loaded the corrplot package) solution:

    Change the place_points function within the corrplot function. To do so, run:

    trace(corrplot, edit=TRUE)
    

    Then replace on line 443

    place_points = function(sig.locs, point) {
      text(pos.pNew[, 1][sig.locs], pos.pNew[, 2][sig.locs], 
           labels = point, col = pch.col, cex = pch.cex, 
           lwd = 2)
    

    with:

    # adjust text(X,Y ...) according to your needs, here +0.25 is added to the Y-position    
    place_points = function(sig.locs, point) {
          text(pos.pNew[, 1][sig.locs], (pos.pNew[, 2][sig.locs])+0.25, 
               labels = point, col = pch.col, cex = pch.cex, 
               lwd = 2)
    

    and then hit the "Save" button.

    Result:

    library(corrplot)
    #> corrplot 0.90 loaded
    
    #change the corrplot function as described above 
    trace(corrplot, edit=TRUE)
    #> Tracing function "corrplot" in package "corrplot"
    #> [1] "corrplot"
    
    M<-cor(mtcars)
    res1 <- cor.mtest(mtcars, conf.level = .95)
    
    corrplot(cor(mtcars),
             method="square",
             type="lower",
             p.mat = res1$p,
             insig = "label_sig",
             sig.level = c(.001, .01, .05),
             pch.cex = 0.8,
             pch.col = "yellow",
             tl.col="black",
             tl.cex=1,
             addCoef.col = "black",
             tl.pos="n",
             outline=TRUE)
    

    Created on 2021-10-13 by the reprex package (v2.0.1)