如何做出相关系数矩阵可视化图

如何做出相关系数矩阵可视化图

1
2
3
4
5
6
7
8
9
10
install.packages("psych")
install.packages("corrplot")#安装包,如果已安装,请略过
library(psych)
library(corrplot)#载入两个包
data(iris)#机器学习常用神奇数据集——鸢尾花数据集
head(iris)#查看下数据集前五行
irisnew<-iris[,-5]#去除第五列种类变量
cormat<-corr.test(irisnew)#相关系数分析及显著性检验
#最简单的相关系数矩阵可视化
corrplot(cormat$r)

corrplot(cormat$r,method="square")

corrplot(cormat$r,method = "number")

corrplot(cormat$r,method = "shade")

corrplot(cormat$r,method="ellipse")

corrplot(cormat$r,method = "pie")

corrplot(cormat$r,method="square",type="lower",title = "Correlation of iris")

#含显著性检验的相关系数矩阵可视化
cormatp<-cormat$p#单独取出p值矩阵
cormatp[upper.tri(cormatp)]=0#设置p值矩阵上三角等于0
corrplot(cormat$r,method="square",type="lower",title = "Correlation of iris",tl.cex=1.5,tl.pos = "lt",number.cex=1,p.mat=cormatp,sig.level=0.05,insig=c("pch"))

corrplot(cormat$r,method="square",type="full",title = "Correlation of iris",tl.cex=1.5,tl.pos = "lt",number.cex=1,p.mat=cormatp,sig.level=0.05,insig=c("pch"))

corrplot.mixed(cormat$r,upper = "square",lower = "number",diag = "u",tl.cex=1.5,tl.pos = "lt",number.cex=1,p.mat=cormatp,sig.level=0.05,insig=c("pch"))

坚持原创技术分享,您的支持将鼓励我继续创作!