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

本文介绍如何使用R语言中的psych和corrplot包进行相关系数矩阵的计算与多种方式的可视化展示,包括简单可视化及结合显著性检验的复杂图表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何在R中优雅地绘制相关系数矩阵

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"))

这里写图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值