使用corrplot包绘制相关性图

使用corrplot包绘制相关性图


加载所需R包

library(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, ...)

使用示例

# 加载数据集
data(mtcars)
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
# 计算相关系数
M <- cor(mtcars)
M
##             mpg        cyl       disp         hp        drat         wt
## mpg   1.0000000 -0.8521620 -0.8475514 -0.7761684  0.68117191 -0.8676594
## cyl  -0.8521620  1.0000000  0.9020329  0.8324475 -0.69993811  0.7824958
## disp -0.8475514  0.9020329  1.0000000  0.7909486 -0.71021393  0.8879799
## hp   -0.7761684  0.8324475  0.7909486  1.0000000 -0.44875912  0.6587479
## drat  0.6811719 -0.6999381 -0.7102139 -0.4487591  1.00000000 -0.7124406
## wt   -0.8676594  0.7824958  0.8879799  0.6587479 -0.71244065  1.0000000
## qsec  0.4186840 -0.5912421 -0.4336979 -0.7082234  0.09120476 -0.1747159
## vs    0.6640389 -0.8108118 -0.7104159 -0.7230967  0.44027846 -0.5549157
## am    0.5998324 -0.5226070 -0.5912270 -0.2432043  0.71271113 -0.6924953
## gear  0.4802848 -0.4926866 -0.5555692 -0.1257043  0.69961013 -0.5832870
## carb -0.5509251  0.5269883  0.3949769  0.7498125 -0.09078980  0.4276059
##             qsec         vs          am       gear        carb
## mpg   0.41868403  0.6640389  0.59983243  0.4802848 -0.55092507
## cyl  -0.59124207 -0.8108118 -0.52260705 -0.4926866  0.52698829
## disp -0.43369788 -0.7104159 -0.59122704 -0.5555692  0.39497686
## hp   -0.70822339 -0.7230967 -0.24320426 -0.1257043  0.74981247
## drat  0.09120476  0.4402785  0.71271113  0.6996101 -0.09078980
## wt   -0.17471588 -0.5549157 -0.69249526 -0.5832870  0.42760594
## qsec  1.00000000  0.7445354 -0.22986086 -0.2126822 -0.65624923
## vs    0.74453544  1.0000000  0.16834512  0.2060233 -0.56960714
## am   -0.22986086  0.1683451  1.00000000  0.7940588  0.05753435
## gear -0.21268223  0.2060233  0.79405876  1.0000000  0.27407284
## carb -0.65624923 -0.5696071  0.05753435  0.2740728  1.00000000
## 设置不同系列颜色
col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white",
                           "cyan", "#007FFF", "blue","#00007F"))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582",
                           "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE",
                           "#4393C3", "#2166AC", "#053061"))
col3 <- colorRampPalette(c("red", "white", "blue"))
col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F",
                           "cyan", "#007FFF", "blue", "#00007F"))
wb <- c("white", "black")
# methold参数设定不同展示方式
corrplot(M) #默认methold="circle"
image.png
corrplot(M, method = "ellipse")
image.png
corrplot(M, method = "pie")
image.png
corrplot(M, method = "color")
image.png
corrplot(M, method = "number")
image.png
# col = "black"设定颜色为黑色, cl.pos = "n"设定颜色图例不展示
corrplot(M, method = "number", col = "black", cl.pos = "n")
image.png
# order参数设定不同展示顺序,默认order="orginal"以原始顺序展示
corrplot(M, order = "AOE")
image.png
# addCoef.col = "grey"添加相关系数并设定颜色为grey
corrplot(M, order = "AOE", addCoef.col = "grey")
image.png
# col = col1(20)设定20种颜色
corrplot(M, order = "AOE", col = col1(20), addCoef.col = "grey")
image.png
corrplot(M, order = "AOE", col = col1(10), addCoef.col = "grey")
image.png
# 更改order = "FPC"
corrplot(M, order = "FPC", col = col2(200))
image.png
corrplot(M, order = "FPC", col = col2(20), addCoef.col = "grey")
image.png
corrplot(M, order = "FPC", col = col2(10), addCoef.col = "grey")
image.png
# 更改order = "hclust"
corrplot(M, order = "hclust", col = col3(100))
image.png
corrplot(M, order = "hclust", col = col3(10))
image.png
## 设定col = wb
corrplot(M, col = wb, order = "AOE", outline = TRUE, cl.pos = "n")
image.png
## bg = "gold2"设置背景色
corrplot(M, col = wb, bg = "gold2",  order = "AOE", cl.pos = "n")
image.png
# 多图组和
## circle + ellipse
# type参数设定展示类型,默认type="full"展示全部
corrplot(M, order = "AOE", type = "upper")
image.png
# tl.pos = "d"设定文本标签展示在对角线
corrplot(M, order = "AOE", type = "upper", tl.pos = "d")
# add = TRUE在已有的图形上添加其他图形
corrplot(M, add = TRUE, type = "lower", method = "ellipse", order = "AOE",
         diag = FALSE, tl.pos = "n", cl.pos = "n")
image.png
# tl.pos = "n"不展示文本标签, cl.pos = "n"不展示颜色图例,diag = FALSE不展示对角线的相关系数
corrplot(M, order = "AOE", type = "lower", method = "ellipse", 
         diag = FALSE, tl.pos = "n", cl.pos = "n")
image.png
## circle + square
corrplot(M, order = "AOE",type = "upper", tl.pos = "d")
corrplot(M, add = TRUE, type = "lower", method = "square", order = "AOE",
         diag = FALSE, tl.pos = "n", cl.pos = "n")
image.png
## circle + colorful number
corrplot(M, order = "AOE", type = "upper", tl.pos = "d")
corrplot(M, add = TRUE, type = "lower", method = "number", order = "AOE",
         diag = FALSE, tl.pos = "n", cl.pos = "n")
image.png
## circle + black number
corrplot(M, order = "AOE", type = "upper", tl.pos = "tp")
corrplot(M, add = TRUE, type = "lower", method = "number", order = "AOE",
         col = "black", diag = FALSE, tl.pos = "n", cl.pos = "n")
image.png
## order is hclust and draw rectangles
corrplot(M, order = "hclust")
image.png
# addrect参数添加矩形方框
corrplot(M, order = "hclust", addrect = 2)
image.png
corrplot(M, order = "hclust", addrect = 3, rect.col = "red")
image.png
corrplot(M, order = "hclust", addrect = 4, rect.col = "blue")
image.png
# hclust.method = "ward.D2"设定聚类方法
corrplot(M, order = "hclust", hclust.method = "ward.D2", addrect = 4)
image.png
## visualize a  matrix in [0, 1]
# cl.lim = c(0,1)设定图例颜色范围
corrplot(abs(M), order = "AOE", cl.lim = c(0,1))
image.png
corrplot(abs(M), order = "AOE", col = col1(20), cl.lim = c(0,1))
image.png
## text-labels and plot type
# tl.srt参数设定文本标签摆放角度
corrplot(M, order = "AOE", tl.srt = 45)
image.png
corrplot(M, order = "AOE", tl.srt = 60)
image.png
corrplot(M, order = "AOE", tl.pos = "d", cl.pos = "n")
image.png
corrplot(M, order = "AOE", type = "upper")
image.png
# diag = FALSE不展示对角线的相关系数
corrplot(M, order = "AOE", type = "upper", diag = FALSE)
image.png
corrplot(M, order = "AOE", type = "lower", cl.pos = "b")
image.png
corrplot(M, order = "AOE", type = "lower", cl.pos = "b", diag = FALSE)
image.png
#### color-legend
# cl.ratio参数设定颜色图例的宽度, cl.align设定图例文本的对齐方式
corrplot(M, order = "AOE", cl.ratio = .1, cl.align = "l")
image.png
corrplot(M, order = "AOE", cl.ratio = .2, cl.align = "l") #居左
image.png
corrplot(M, order = "AOE", cl.ratio = .2, cl.align = "c") #居中
image.png
corrplot(M, order = "AOE", cl.ratio = .2, cl.align = "r") #居右
image.png
# cl.pos设定颜色图例的位置,tl.pos设定文本标签位置
corrplot(M, order = "AOE", cl.pos = "b")
image.png
corrplot(M, order = "AOE", cl.pos = "b", tl.pos = "d")
image.png
corrplot(M, order = "AOE", cl.pos = "n")
image.png
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: OS X El Capitan 10.11.3
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] corrplot_0.84
## 
## loaded via a namespace (and not attached):
##  [1] compiler_3.5.1  backports_1.1.2 magrittr_1.5    rprojroot_1.3-2
##  [5] tools_3.5.1     htmltools_0.3.6 yaml_2.2.0      Rcpp_0.12.18   
##  [9] stringi_1.2.4   rmarkdown_1.10  knitr_1.20      stringr_1.3.1  
## [13] digest_0.6.16   evaluate_0.11
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,129评论 25 707
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,723评论 2 59
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_X自主阅读 15,980评论 3 119
  • 人家孩子总是不爱阅读,我们妞妞则是痴迷阅读。妈妈很少推荐她读什么,更不存在强制她读什么。反之,一再的追问...
    菜鸟归来阅读 203评论 0 0
  • 楚汉争霸,河界两分。 探子来报;“王,汉营来袭,我方溃不成军。” 项羽身形笃定;“昔日西楚,有我霸王力举铜鼎,震慑...
    陆临霜阅读 965评论 1 1