R语言可视化(十五):相关性图绘制

15. 相关性图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/15correlation/")

使用corrgram包绘制相关性图

library(corrgram)

# 查看数据集
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

corrgram(iris)
R语言可视化(十五):相关性图绘制_第1张图片
image.png
corrgram(iris, 
         lower.panel=panel.pts, #设置底部panel绘图类型
         upper.panel=panel.conf, #设置顶部panel绘图类型
         diag.panel=panel.density, #设置对角线panel绘图类型
         main = "Iris data pearson correlation" #设置标题
         )
R语言可视化(十五):相关性图绘制_第2张图片
image.png
corrgram(iris, 
         lower.panel=panel.shade, 
         upper.panel=panel.pie,
         diag.panel=panel.density,
         cor.method = "spearman", #设置相关性计算方法
         gap = 2, #设置图形panel之间的间隔
         col.regions=colorRampPalette(c("green", "blue","red"))
         )
R语言可视化(十五):相关性图绘制_第3张图片
image.png

使用corrplot包绘制相关性图

library(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

# 计算相关性
M <- cor(mtcars,method = "pearson")
head(M)
            mpg        cyl       disp         hp       drat         wt
mpg   1.0000000 -0.8521620 -0.8475514 -0.7761684  0.6811719 -0.8676594
cyl  -0.8521620  1.0000000  0.9020329  0.8324475 -0.6999381  0.7824958
disp -0.8475514  0.9020329  1.0000000  0.7909486 -0.7102139  0.8879799
hp   -0.7761684  0.8324475  0.7909486  1.0000000 -0.4487591  0.6587479
drat  0.6811719 -0.6999381 -0.7102139 -0.4487591  1.0000000 -0.7124406
wt   -0.8676594  0.7824958  0.8879799  0.6587479 -0.7124406  1.0000000
            qsec         vs         am       gear       carb
mpg   0.41868403  0.6640389  0.5998324  0.4802848 -0.5509251
cyl  -0.59124207 -0.8108118 -0.5226070 -0.4926866  0.5269883
disp -0.43369788 -0.7104159 -0.5912270 -0.5555692  0.3949769
hp   -0.70822339 -0.7230967 -0.2432043 -0.1257043  0.7498125
drat  0.09120476  0.4402785  0.7127111  0.6996101 -0.0907898
wt   -0.17471588 -0.5549157 -0.6924953 -0.5832870  0.4276059

corrplot(M)
R语言可视化(十五):相关性图绘制_第4张图片
image.png
corrplot(M,
         method = "number", #设置相关性图展示类型
         type = "lower", #设置只展示底部panel
         bg = "white", #设置背景色
         title = "mtcars data correlation", #设置标题
         )
R语言可视化(十五):相关性图绘制_第5张图片
image.png
corrplot(M,
         method = "pie", #设置相关性图展示类型
         type = "upper", #设置只展示底部panel
         order = "AOE", #设置排序的方式
         cl.ratio = .2, #设置colorlabel的宽度
         title = "mtcars data pearson correlation", #设置标题
         )
R语言可视化(十五):相关性图绘制_第6张图片
image.png

使用ggcorrplot包绘制相关性图

library(ggcorrplot)

# 查看数据集
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,method = "spearman")
head(M)
            mpg        cyl       disp         hp       drat         wt
mpg   1.0000000 -0.9108013 -0.9088824 -0.8946646  0.6514555 -0.8864220
cyl  -0.9108013  1.0000000  0.9276516  0.9017909 -0.6788812  0.8577282
disp -0.9088824  0.9276516  1.0000000  0.8510426 -0.6835921  0.8977064
hp   -0.8946646  0.9017909  0.8510426  1.0000000 -0.5201250  0.7746767
drat  0.6514555 -0.6788812 -0.6835921 -0.5201250  1.0000000 -0.7503904
wt   -0.8864220  0.8577282  0.8977064  0.7746767 -0.7503904  1.0000000
            qsec         vs         am       gear       carb
mpg   0.46693575  0.7065968  0.5620057  0.5427816 -0.6574976
cyl  -0.57235095 -0.8137890 -0.5220712 -0.5643105  0.5800680
disp -0.45978176 -0.7236643 -0.6240677 -0.5944703  0.5397781
hp   -0.66660602 -0.7515934 -0.3623276 -0.3314016  0.7333794
drat  0.09186863  0.4474575  0.6865708  0.7448162 -0.1252229
wt   -0.22540120 -0.5870162 -0.7377126 -0.6761284  0.4998120

ggcorrplot(M)
R语言可视化(十五):相关性图绘制_第7张图片
image.png
ggcorrplot(M,
           method = "circle", #设置相关性图展示类型
           outline.color = "red",#设置相关性图边框的颜色
           type = "upper", #设置只展示定部panel
           title = "mtcars data spearman correlation" #设置标题
           )
R语言可视化(十五):相关性图绘制_第8张图片
image.png
ggcorrplot(M,
           method = "square", #设置相关性图展示类型
           show.legend = T, #设置是否展示图例
           legend.title = "Corr", #设置图例的标题
           colors = c("#6D9EC1", "white", "#E46726"), #设置相关性图的颜色
           ggtheme = ggplot2::theme_gray, #设置背景
           lab = T, #设置是否显示显关系数
           hc.order = T #设置排序
           )
R语言可视化(十五):相关性图绘制_第9张图片
image.png

使用GGally包绘制相关性图

library(GGally)

# 查看数据集
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,method = "kendall")
head(M)
            mpg        cyl       disp         hp       drat         wt
mpg   1.0000000 -0.7953134 -0.7681311 -0.7428125  0.4645488 -0.7278321
cyl  -0.7953134  1.0000000  0.8144263  0.7851865 -0.5513178  0.7282611
disp -0.7681311  0.8144263  1.0000000  0.6659987 -0.4989828  0.7433824
hp   -0.7428125  0.7851865  0.6659987  1.0000000 -0.3826269  0.6113081
drat  0.4645488 -0.5513178 -0.4989828 -0.3826269  1.0000000 -0.5471495
wt   -0.7278321  0.7282611  0.7433824  0.6113081 -0.5471495  1.0000000
            qsec         vs         am       gear        carb
mpg   0.31536522  0.5896790  0.4690128  0.4331509 -0.50439455
cyl  -0.44896982 -0.7710007 -0.4946212 -0.5125435  0.46542994
disp -0.30081549 -0.6033059 -0.5202739 -0.4759795  0.41373600
hp   -0.47290613 -0.6305926 -0.3039956 -0.2794458  0.59598416
drat  0.03272155  0.3751011  0.5755485  0.5839248 -0.09535193
wt   -0.14198812 -0.4884787 -0.6138790 -0.5435956  0.37137413

# 使用ggcorr函数绘制相关性图
ggcorr(M)
R语言可视化(十五):相关性图绘制_第10张图片
image.png
ggcorr(M,
       label = T, #设置是否显示相关系数
       geom = "circle", #设置相关性图展示类型
       max_size = 10, #设置circles size的最大值
       min_size = 4, #设置circles size的最小值
       size = 4, #设置对角线字体大小
       angle = 45, #设置对角线字体倾斜角度
       low = "green",
       mid = "blue",
       high = "red"
       )
R语言可视化(十五):相关性图绘制_第11张图片
image.png
sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 
[2] LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] GGally_1.4.0     ggcorrplot_0.1.3 ggplot2_3.2.0    corrplot_0.84   
[5] corrgram_1.13   

loaded via a namespace (and not attached):
 [1] gtools_3.8.1       xfun_0.8           tidyselect_0.2.5  
 [4] purrr_0.3.2        reshape2_1.4.3     colorspace_1.4-1  
 [7] htmltools_0.3.6    viridisLite_0.3.0  yaml_2.2.0        
[10] rlang_0.4.7        pillar_1.4.2       glue_1.3.1        
[13] withr_2.1.2        RColorBrewer_1.1-2 registry_0.5-1    
[16] foreach_1.4.4      plyr_1.8.4         stringr_1.4.0     
[19] munsell_0.5.0      gtable_0.3.0       caTools_1.17.1.2  
[22] evaluate_0.14      codetools_0.2-16   knitr_1.23        
[25] labeling_0.3       seriation_1.2-7    Rcpp_1.0.5        
[28] KernSmooth_2.23-15 scales_1.0.0       gdata_2.18.0      
[31] gplots_3.0.1.1     gridExtra_2.3      digest_0.6.20     
[34] stringi_1.4.3      gclus_1.3.2        dplyr_0.8.3       
[37] grid_3.6.0         tools_3.6.0        bitops_1.0-6      
[40] magrittr_1.5       lazyeval_0.2.2     tibble_2.1.3      
[43] cluster_2.0.8      crayon_1.3.4       pkgconfig_2.0.2   
[46] dendextend_1.12.0  MASS_7.3-51.4      rmarkdown_1.13    
[49] assertthat_0.2.1   reshape_0.8.8      rstudioapi_0.10   
[52] iterators_1.0.10   viridis_0.5.1      R6_2.4.0          
[55] TSP_1.1-7          compiler_3.6.0

你可能感兴趣的:(R语言可视化(十五):相关性图绘制)