使用corrplot包繪制相關(guān)性圖

使用corrplot包繪制相關(guān)性圖


加載所需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, ...)

使用示例

# 加載數(shù)據(jù)集
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
# 計(jì)算相關(guān)系數(shù)
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
## 設(shè)置不同系列顏色
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參數(shù)設(shè)定不同展示方式
corrplot(M) #默認(rèn)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"設(shè)定顏色為黑色, cl.pos = "n"設(shè)定顏色圖例不展示
corrplot(M, method = "number", col = "black", cl.pos = "n")
image.png
# order參數(shù)設(shè)定不同展示順序涣澡,默認(rèn)order="orginal"以原始順序展示
corrplot(M, order = "AOE")
image.png
# addCoef.col = "grey"添加相關(guān)系數(shù)并設(shè)定顏色為grey
corrplot(M, order = "AOE", addCoef.col = "grey")
image.png
# col = col1(20)設(shè)定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
## 設(shè)定col = wb
corrplot(M, col = wb, order = "AOE", outline = TRUE, cl.pos = "n")
image.png
## bg = "gold2"設(shè)置背景色
corrplot(M, col = wb, bg = "gold2",  order = "AOE", cl.pos = "n")
image.png
# 多圖組和
## circle + ellipse
# type參數(shù)設(shè)定展示類(lèi)型藻茂,默認(rèn)type="full"展示全部
corrplot(M, order = "AOE", type = "upper")
image.png
# tl.pos = "d"設(shè)定文本標(biāo)簽展示在對(duì)角線
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"不展示文本標(biāo)簽, cl.pos = "n"不展示顏色圖例宴卖,diag = FALSE不展示對(duì)角線的相關(guān)系數(shù)
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參數(shù)添加矩形方框
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"設(shè)定聚類(lèi)方法
corrplot(M, order = "hclust", hclust.method = "ward.D2", addrect = 4)
image.png
## visualize a  matrix in [0, 1]
# cl.lim = c(0,1)設(shè)定圖例顏色范圍
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參數(shù)設(shè)定文本標(biāo)簽擺放角度
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不展示對(duì)角線的相關(guān)系數(shù)
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參數(shù)設(shè)定顏色圖例的寬度, cl.align設(shè)定圖例文本的對(duì)齊方式
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設(shè)定顏色圖例的位置,tl.pos設(shè)定文本標(biāo)簽位置
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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末建车,一起剝皮案震驚了整個(gè)濱河市坐漏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件箭启,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蛉迹,警方通過(guò)查閱死者的電腦和手機(jī)傅寡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人荐操,你說(shuō)我怎么就攤上這事芜抒。” “怎么了托启?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵宅倒,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我屯耸,道長(zhǎng)拐迁,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任疗绣,我火速辦了婚禮线召,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘多矮。我一直安慰自己缓淹,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布塔逃。 她就那樣靜靜地躺著割卖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪患雏。 梳的紋絲不亂的頭發(fā)上鹏溯,一...
    開(kāi)封第一講書(shū)人閱讀 49,144評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音淹仑,去河邊找鬼丙挽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛匀借,可吹牛的內(nèi)容都是我干的颜阐。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼吓肋,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼凳怨!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起是鬼,我...
    開(kāi)封第一講書(shū)人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤肤舞,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后均蜜,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體李剖,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年囤耳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了篙顺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片偶芍。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖德玫,靈堂內(nèi)的尸體忽然破棺而出匪蟀,到底是詐尸還是另有隱情,我是刑警寧澤宰僧,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布萄窜,位于F島的核電站,受9級(jí)特大地震影響撒桨,放射性物質(zhì)發(fā)生泄漏查刻。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一凤类、第九天 我趴在偏房一處隱蔽的房頂上張望穗泵。 院中可真熱鬧,春花似錦谜疤、人聲如沸佃延。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)履肃。三九已至,卻和暖如春坐桩,著一層夾襖步出監(jiān)牢的瞬間尺棋,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工绵跷, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留膘螟,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓碾局,卻偏偏與公主長(zhǎng)得像荆残,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子净当,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,522評(píng)論 25 707
  • 用兩張圖告訴你内斯,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 12,693評(píng)論 2 59
  • 1像啼、通過(guò)CocoaPods安裝項(xiàng)目名稱(chēng)項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明先生_x閱讀 15,968評(píng)論 3 119
  • 人家孩子總是不愛(ài)閱讀俘闯,我們妞妞則是癡迷閱讀。媽媽很少推薦她讀什么埋合,更不存在強(qiáng)制她讀什么备徐。反之萄传,一再的追問(wèn)...
    菜鳥(niǎo)歸來(lái)閱讀 196評(píng)論 0 0
  • 楚漢爭(zhēng)霸甚颂,河界兩分蜜猾。 探子來(lái)報(bào);“王振诬,漢營(yíng)來(lái)襲蹭睡,我方潰不成軍「厦矗” 項(xiàng)羽身形篤定肩豁;“昔日西楚,有我霸王力舉銅鼎辫呻,震懾...
    陸臨霜閱讀 960評(píng)論 1 1