如何在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"))
這里寫圖片描述