二更~
corrplot我觉得有一点不好就是改变不了图的纵横比,其他一流
library(corrplot)
head(mydata)
1. 构建相关系数矩阵
y=matrix(NA,10,5) #我的数据里‘1’到‘10’列为行,‘A’到‘E’为列,为10×5矩阵
for (i in 1:10){
for (j in 11:15){
y[i,j-10]=cor(mydata[,i],mydata[,j])
}
j=11
}
#命名相关系数矩阵
names1=names(mydata)
colnames(y)=names1[11:15]
rownames(y)=names1[1:10]
2. 构建P值矩阵
z=matrix(NA,10,5)
for (i in 1:10){
for (j in 11:15){
a=cor.test(mydata[,i],mydata[,j])
z[i,j-10]=a$p.value
}
j=11
}
#命名P值矩阵
names1=names(mydata)
colnames(z)=names1[11:15]
rownames(z)=names1[1:10]
3. corrplot绘制相关系数热图
library(RColorBrewer)
coul=colorRampPalette(c('darkblue','white','darkred'))(20)
corrplot(y,
col = coul,
#修改横纵坐标字体样式
tl.srt = 0,
tl.cex = 1,
tl.col = 'black',
tl.offset = 0.5,
#修改legend样式
cl.lim = c(-0.3,0.3),
cl.align.text = 'l',
cl.length = 5,
cl.ratio = 0.1,
cl.cex = 0.8,
#网格线颜色和方块显示
addgrid.col = 'white',
method = 'color',
#p值及其样式
p.mat=z,
insig='label_sig',
sig.level = c(0.001,0.01,0.05),
pch.cex = 2,
#非正方形矩阵
is.corr = FALSE,
#其他格式
mar = c(0,0,2,2),
xpd = T #这个很重要,不信你试试
)