我如何只在相关矩阵中包括大于0.6的相关系数?

时间:2018-07-09 15:39:48

标签: r correlation

我试图在R的相关矩阵中仅绘制相关系数大于.6的关系。当前,我正在使用包'corr_test'来运行Pearson Correlation,然后使用'ggcorrplot'来创建矩阵。如何修改代码以仅绘制比+.6 /-。6强的相关系数?

下面是我当前的代码:

##New Pearson Correlation Script##
#Load in the stuff you dont have
install.packages("psych")
install.packages("ggcorrplot")
#Load it up
library("psych")
library("ggcorrplot")
library("ggplot2")
##Load in your datasets, just fill D1 if one matrix, load D2 as well if 2 matrices
D1 =class
D2 =metab
#Run the correlation
test =corr.test(D1, y = D2, use = "complete")
#r is the correlation matrix 
r = test$r
#p is the p-values
p = test$p

#plot it out 
testplot= ggcorrplot(r, method = "circle" ,lab_size= 0.3, tl.cex = 8, outline.col = "black", tl.col = "black")+
  theme(axis.text.y=element_text(size=9),
        axis.text.x = element_text(size=9, angle=90))
print(testplot)

单击Here(Google云端硬盘文件)以获取我一直在使用的相关系数的小型数据集。

谢谢!

2 个答案:

答案 0 :(得分:1)

最后,这非常简单,只需将所有绝对值都低于0.6的值设置为NA

r2 <- r    # Save a copy in case you need it later
is.na(r) <- abs(r) < 0.6

#plot it out 
testplot= ggcorrplot(r, method = "circle" ,lab_size= 0.3, tl.cex = 8, outline.col = "black", tl.col = "black")+
  theme(axis.text.y=element_text(size=9),
        axis.text.x = element_text(size=9, angle=90))
print(testplot)

r <- r2    # reset
rm(r2)     # tidy up

enter image description here

答案 1 :(得分:0)

在绘制之前,请基于绝对值大于或等于0.6的子集。

r <- subset(r, abs(r) >= .6)