Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the position of the signifigance pch symbols in `corrplot()`?

Tags:

plot

r

r-corrplot

The script below produces a plot in which the pch symbols for significance overlap the r values. How do you shift the position of the pch symbols so that they do not overlap these values?

library(corrplot)

ex.mat <- matrix(c(1.00,0.46,-0.75,1.00,0.46,1.00,0.00,0.46,-0.75,0.00,1.00,-0.75,1.00,0.46,-0.75,1.00), nrow = 4, ncol = 4)
ex.pmat <- matrix(c(NA,0.2939,0.0522,0.0000,0.2939,NA,1.0000,0.2939,0.0522,1.0000,NA,0.0522,0.0000,0.2939,0.0522,NA), nrow = 4, ncol = 4)

corrplot(ex.mat, p.mat = ex.pmat ,sig.level = c(.001, .01, .05), type = "upper", 
         insig = "label_sig", pch.cex = 1.5,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")

It would be ideal to be able to automate this, but it can be done manually:

ex.mat <- matrix(c(1.00,0.46,-0.75,1.00,0.46,1.00,0.00,0.46,-0.75,0.00,1.00,-0.75,1.00,0.46,-0.75,1.00), nrow = 4, ncol = 4)
ex.pmat <- matrix(c(NA,0.2939,0.0522,0.0000,0.2939,NA,1.0000,0.2939,0.0522,1.0000,NA,0.0522,0.0000,0.2939,0.0522,NA), nrow = 4, ncol = 4)

corrplot(ex.mat, type = "upper", 
         insig = "label_sig", pch.cex = 1.5, cl.length = 3,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")


points(4.35, 4.25 , type = "p", pch = "*", cex = 2, col = "ivory")
points(4.20, 4.25 , type = "p", pch = "*", cex = 2, col = "ivory")
points(4.05, 4.25 , type = "p", pch = "*", cex = 2, col = "ivory")
like image 803
JKO Avatar asked Oct 29 '25 01:10

JKO


1 Answers

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 are displayed, they overlap.

# library
library(corrplot)
#> corrplot 0.90 loaded

# data
ex.mat <- matrix(c(1.00,0.46,-0.75,1.00,0.46,1.00,0.00,0.46,-0.75,0.00,1.00,-0.75,1.00,0.46,-0.75,1.00), nrow = 4, ncol = 4)

#since your example threw an error with the actual corrplot package I slightly edited you code

#set colnames
colnames(ex.mat) <- c("A","B","C","D")

# calculate p-values
ex.pmat <- cor.mtest(ex.mat, conf.level = .95)

# overlapping plot
corrplot(ex.mat, p.mat = ex.pmat$p ,sig.level = c(.001, .01, .05), type = "upper", 
         insig = "label_sig", pch.cex = 1.5,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")

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:

# chance the corrplot function as described above
trace(corrplot, edit=TRUE)
#> Tracing function "corrplot" in package "corrplot"
#> [1] "corrplot"

# non-overlapping plot
corrplot(ex.mat, p.mat = ex.pmat$p ,sig.level = c(.001, .01, .05), type = "upper", 
         insig = "label_sig", pch.cex = 1.5,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")

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

like image 114
captcoma Avatar answered Oct 30 '25 16:10

captcoma