我如何在相关矩阵中只包含高于 .6 的相关系数?
How would I only include correlation coefficients above .6 in a correlation matrix?
我试图在 R 的相关矩阵中仅绘制相关系数高于 .6 的关系。目前,我正在使用包 'corr_test' 到 运行 皮尔逊相关,然后我使用 '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 驱动文件)获取我一直在使用的相关系数的小型数据集。
谢谢!
在绘图之前,根据绝对值大于或等于 0.6 进行子集化。
r <- subset(r, abs(r) >= .6)
最后很简单,把所有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
我试图在 R 的相关矩阵中仅绘制相关系数高于 .6 的关系。目前,我正在使用包 'corr_test' 到 运行 皮尔逊相关,然后我使用 '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 驱动文件)获取我一直在使用的相关系数的小型数据集。
谢谢!
在绘图之前,根据绝对值大于或等于 0.6 进行子集化。
r <- subset(r, abs(r) >= .6)
最后很简单,把所有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