Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

r - corrplot blank and no text for insignificant values

According to the documentation here it is possible to customize the behavior of insignificant values, to hide them.

documentation:

The doc tells us how to do two things I'm interested in:

  • correlation coefficient (addCoef.col = "black")
  • blank insignificant (sig.level=0.01, insig="blank")

Problem:

when using the three above tags (along with others, complete list below) the correlation coefficients also appear for insignificant values.

What I want:

  • have correlation coefficients and colors for all boxes except the insignificant ones
  • insig boxes must be totally empty

What I do:

cr<-colorRampPalette(c("lightblue","white","yellow"))(200)
p <- cor.mtest(dataCor)
corMat=cor(dataCor)

corrplot(corMat, type="upper",method="color",order="original"
            , col=cr
            , tl.col="black"
            , addCoef.col="black",
            , diag=FALSE,number.cex=.7
            , insig="blank"
            , p.mat = p,sig.level=0.01,tl.srt = 45)

results in (extract): correlation plot The goal is to remove the "-0.01" of prop02 x prop04 and all the zeroes

EDIT: I know the props are not in order, in my case it's on purpose (they have different names and are grouped in a relevant way)

UPDATE: I found this thread: corrplot shows insignificant correlation coefficients even when insig = "blank" is set

It "works" (still a dirty fix) but only for square matrices with diagonals. How to make it work for type="upper" and diag=FALSE?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way to tackle the problem is to create a color matrix containing each cell's text color.

The addCoef.col argument can take a matrix. This matrix has to correspond to the final shape given by the other arguments diag and type (in this case : diag=TRUE,type=upper)

One way to create this matrix is by creating it from the p-value matrix as follows.

pval <- 0.01 #threshold
p <- cor.mtest(dataCor) # compute p-values
corMat <- cor(dataCor) # compute correlation values

#create the color matrix from the p-value matrix, select only necessary data
mycol <-ifelse(c(p > pval), "white", "black")[upper.tri(p, diag = FALSE)]

What's happening here:

  • ifelse will return the correponding branch (white or black here) given the logical values in the first argument.
  • c(p > pval) creates a logical array with TRUE when the value in the p array is bigger than the p-value threshold. These are the values to mask. Technically, the condition can be more complex: c(p > pval | abs(corMat)>0.5). In this case there is a condition on both the pvalue and the correlation coefficient. It can be useful for contrast issues, if the color ramp is too dark for high correlation, white text should be used.
  • [upper.tri(p, diag = FALSE)] serves to select only the values we are interested in. upper.tri() returns an array of logical, with TRUE when the value belongs to the desired upper side of the matrix.^

Presenting values from -100 to 100 is better for readability and understanding. Using addCoefasPercent=TRUE,cl.lim = c(-100, 100) in the corrplot arguments is arguably better in some cases.

corrected color scheme

look at this answer for how to re-order (or keep ordering) i.e., what you specify with the order argument (possible values are "AOE", "FPC", "hclust", "alphabet".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...