背景介绍
最近在做一些数据处理工作,需要对一些数据进行可视化。R语言的corrplot包能够优雅地将数据的相关性可视化出来,实现数据的相关性展示。 今天我们来介绍一下R语言中corrplot包的使用教程。
软件介绍
[软件名称]:R&RStudio
绘图教程
1.首先安装需要的R包
install.packages("corrplot")
library(corrplot)
2.这里,我们使用R的自带数据mtcars进行绘图,本教程也是基于corrplot包的示例文档,略微进行了修改。
head(mtcars)
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
3.首先我们看一下corrplot的用法,这是函数中的所有可调节参数
corrplot(
corr,
method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
type = c("full", "lower", "upper"),
col = NULL,
col.lim = NULL,
bg = "white",
title = "",
is.corr = TRUE,
add = FALSE,
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.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,
...
)
4.我们还是根据example进行学习吧,首先我们对mtcars数据计算其相关性
M <- cor(mtcars)
head(M)
# mpg cyl disp hp drat wt qsec vs am gear carb
# mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.41868403 0.6640389 0.5998324 0.4802848 -0.5509251
# cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.6999381 0.7824958 -0.59124207 -0.8108118 -0.5226070 -0.4926866 0.5269883
# disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.7102139 0.8879799 -0.43369788 -0.7104159 -0.5912270 -0.5555692 0.3949769
# hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.4487591 0.6587479 -0.70822339 -0.7230967 -0.2432043 -0.1257043 0.7498125
# drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.0000000 -0.7124406 0.09120476 0.4402785 0.7127111 0.6996101 -0.0907898
# wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.7124406 1.0000000 -0.17471588 -0.5549157 -0.6924953 -0.5832870 0.4276059
5.先做个基础的相关性热图出来
corrplot(M)
6.通过method,可以更改其他类型的相关性热图
corrplot(M,method = "number")
corrplot(M,method = "square") #方形
corrplot(M,method = "pie") #饼图
7.通过设置type参数,可以显示完整的图形,或者上半部分,下半部分
corrplot(M,type = "lower") # 下半部分
corrplot(M,type = "upper") #上半部分
8.背景默认为白色,你也可以通过bg参数进行调整
corrplot(M,bg = "red") #背景为红色
corrplot(M,bg = "blue") #背景为蓝色
corrplot(M,bg = 'gold2') #背景为金色
9.如果可以将相关系数添加在图中更好,更加直观
corrplot(M, addCoef.col = 'black')
10.图形默认为按照原始数据进行绘图,你也可以设置order,按照AOE排序,即特征向量的角度顺序
corrplot(M, order = "AOE", addCoef.col = 'black')
11.你也可以对图形的颜色进行自定义(想设置几个就设置几个),不过默认的颜色就很好看了,不建议更改
wb = c("blue","gray","black","red")
corrplot(M, col = wb)
12.如果想要一半数字,一半图形呢?
corrplot(M)
corrplot(M,
add = TRUE, #增加一个新图
type = 'lower', #在左下角
method = 'number', # 类型为数字
tl.pos = 'n', #不添加文字标签
cl.pos = 'n') #不绘制底数字矩阵图的图例
13.基本上上述内容,就够你做个简单的相关性热图了。如果需要更复杂的图形设置,可以继续学习example中的例子,对corrplot函数进行详细学习。