ref: R画月亮阴晴圆缺:corrplot绘图相关系数矩阵
Correlation【相关系数】, Variance【方差】 and Covariance【协方差】 (Matrices)
求相关系数:
n <- 10
x <- rnorm(n)
y <- rnorm(n)
# method=c("pearson", "kendall", "spearman")
cor(x, y,method = "pearson") #相关系数
cor.test(x,y)
可视化方法一:
#例子:

library(corrplot)
M = cor(mtcars)
corrplot(M,add=TRUE, type="lower", method="number",order="AOE",diag=FALSE,tl.pos="n", cl.pos="n")
很详细的R包介绍-ref: R语言相关系数可视化之corrplot包 - 知乎

画图:

可视化方法二:
library(PerformanceAnalytics)#加载包
chart.Correlation(mydata, histogram=TRUE, pch=19)

可视化方法二:
heatmap()
col<- colorRampPalette(c("blue","white","red"))(20)#调用颜色版自定义颜色
heatmap(x = res, col = col, symm =TRUE)#symm表示是否对称

网友评论