R语言相关系数图corplot怎样只显示下半边

Python013

R语言相关系数图corplot怎样只显示下半边,第1张

画上三角矩阵   corrplot(M, type = "upper")

供参考。

corrplot中参数详解

corrplot(corr, method = c("circle", "square", "ellipse", "number", "shade",

"color", "pie"), type = c("full", "lower", "upper"), add = FALSE,

col = NULL, bg = "white", title = "", is.corr = TRUE, diag = TRUE,

outline = FALSE, mar = c(0, 0, 0, 0), addgrid.col = NULL,

addCoef.col = NULL, addCoefasPercent = FALSE, order = c("original",

"AOE", "FPC", "hclust", "alphabet"), hclust.method = c("complete", "ward",

"ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid"),

addrect = NULL, rect.col = "black", rect.lwd = 2, tl.pos = NULL,

tl.cex = 1, tl.col = "red", tl.offset = 0.4, tl.srt = 90,

cl.pos = NULL, cl.lim = NULL, cl.length = NULL, cl.cex = 0.8,

cl.ratio = 0.15, cl.align.text = "c", cl.offset = 0.5, number.cex = 1,

number.font = 2, number.digits = NULL, addshade = c("negative",

"positive", "all"), shade.lwd = 1, shade.col = "white", p.mat = NULL,

sig.level = 0.05, insig = c("pch", "p-value", "blank", "n", "label_sig"),

pch = 4, pch.col = "black", pch.cex = 3, plotCI = c("n", "square",

"circle", "rect"), lowCI.mat = NULL, uppCI.mat = NULL, na.label = "?",

na.label.col = "black", win.asp = 1, ...)

是不是有点多,功能实在太强大了,我们挑几个常用的说明。

corr: 用于绘图的矩阵,必须是正方形矩阵,如果是普通的矩阵,需要设置is.corr=FALSE

method: 可视化的方法,默认是圆circle,还有正方形square、椭圆ellipse、数字number、阴影shade、颜色color和饼pie可选。文章开篇处的示例即为饼形,类似月亮周期的大小变化。

type:展示类型,默认全显full,还有下三角lower,或上三角upper可选。

col:颜色设置,可设置颜色起、中、终点颜色。

is.corr:逻辑值,若为TRUE,不强制要求矩阵是正方形的相关系数矩阵,图例范围也会随数据变化

diag:是否显示对角线值。因为对角线全为1,显示只是美观,实际意义不大

tl.cex:名称标签字体大小

tl.col:名称标签字体颜色

cl.lim:值域范围

addrect:设置分组数量,添加矩形

method – 呈现方式,包括circle(默认) – 圆形,square – 方块,ellipse – 椭圆,number – 数字,pie – 饼图,shade – 阴影,color – 颜色;

diag – 是否画对角线,这里设置为不画;

type – 画图的哪一部分,包括full – 全部,lower – 下三角,upper – 上三角;

col – 颜色,默认为由红到蓝,格式为c(bottom, middle, top),bottom – -1处的颜色,middle – 0处的颜色,top – 1处的颜色;这里先用colorRampPalette生成了颜色设置函数col3(number), number – colorbar上分段的数量,这里分了20短,即每0.1为一段;

cl.lim – 颜色的范围,默认为-1到1,注意设置的范围要包含矩阵中的所有数据;

addgrid.col – 网格的颜色,默认为灰色,这里设置为NA,即不画网格;

tl.pos – 坐标轴标签的位置,包括lt – 左边和上边,ld – 左边和对角线,td – 上边和对角线,d – 对角线,n – 不画;这里设置为lt,即行变量名在左边,列变量名在上边;

tl.cex – 标签的大小;

tl.col – 标签的颜色;

tl.srt – 标签旋转的角度;

tl.offset – 标签和图片的相对位置;

hclust.method – 相关系数聚类的方法,默认不聚类,具体方法包括"ward", "ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid",具体读者可自行尝试

outline – 是否画圆圈的边界,默认不画,可为逻辑变量或字符变量(字符变量为设置边界的颜色);

title – 图片的标题。

以上为corrplot()常用的参数,基本上可供读者做出漂亮的相关矩阵图了。

mat =as.data.frame(diag(9))

for( i in 1:9){

 for(j in 1:9)

if(i>=j)

 {mat[i,j]=paste(i,'x',j,'=',i*j)

}

else {mat[i,j]=''}

}

mat

运行上述就可以得出