[英]How would I only include correlation coefficients above .6 in a correlation matrix?
我试图在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)
单击此处 (Google云端硬盘文件),获取我一直在使用的相关系数的小型数据集。
谢谢!
最后,这很简单,只需将绝对值低于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
在绘制之前,请基于绝对值大于或等于0.6的子集。
r <- subset(r, abs(r) >= .6)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.