taiyun / corrplot

A visual exploratory tool on correlation matrix
https://github.com/taiyun/corrplot
Other
316 stars 86 forks source link

Opposite correlation coefficient using corrplot and stat_cor() #163

Closed Yale73 closed 3 years ago

Yale73 commented 3 years ago

Hi corrplot team,

Thanks for such a great package to visualize the correlation matrix. Recently, I am using this package to analyze my single-cell data. However, I found the results I got were opposite to that of stat_cor().

I am sure I used the same object and the same cluster. But when I made the single cell data matrix and used the following code for the significant filter.

genes <- c("TNFRSF4", "TNFRSF18", "IL2RA")
C3 <- as.data.frame(Treg3@assays[["RNA"]]@data[genes, ])
C31 <- as.data.frame(t(C3))
C32<-cor(C31)

cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}
# matrix of the p-value of the correlation
p.mat <- cor.mtest(C32)

corrplot(C32, type="upper", order="hclust", tl.col="black", tl.srt=45, title="eTreg",
         p.mat = p.mat, sig.level = 0.05, insig = "blank", mar=c(0,0,1,0))

I got TNFRSF4 and TNFRSF18 are positively correlated but negatively correlated with IL2RA (see the pale pink dots). image

But when I used the Seurat embedded FeatureScatter() with stat_cor(), I found all three are positively correlated (R is person correlation coefficient, while p is p-value)

FeatureScatter( object = Treg3, feature1  = "IL2RA", feature2 = "TNFRSF4", cols = "khaki")+
stat_cor(method = "pearson") 

image

I also checked my data with before and after treatment and found they are definitely positively correlated.

Can you help me fix it? Whether it is because of the cor.mtest() function?

Thanks, Yale

taiyun commented 3 years ago

@Yale73 tell your corrplot version. I suggest corrplot v0.88.

cor.mtest() function is OK. The previous version 0.85, 0.87 has a serious bug by default setting full_col = TRUE. Just like #152 #157 #165 #166 The bug was fixed in v0.88. You can get it on CRAN or github.

@vsimko