美文网首页基本图形绘制R plot
ggcorrplot | 简单的相关性热图绘制

ggcorrplot | 简单的相关性热图绘制

作者: 木舟笔记 | 来源:发表于2021-04-01 11:23 被阅读0次
    ggcorrplot_cover.jpg

    ggcorrplot的基础用法指南

    ggcorrplot

    ggcorrplot提供了一种对相关矩阵进行重新排序的解决方案,并在相关图上显示显著性水平。它还包括一个用于计算相关p值矩阵的函数。

    安装和加载

    install.packages("ggcorrplot")
    library(ggcorrplot)
    

    计算相关性矩阵

    #以内置的mtcars数据集为例
    data(mtcars)
    #数据集格式如下
    
    mtcars
    #cor()函数可以非常方便快捷的计算出连续变量之间的相关系数、
    corr <- round(cor(mtcars), 1)
    head(corr[, 1:6])
    
    > head(corr[, 1:6])
          mpg  cyl disp   hp drat   wt
    mpg   1.0 -0.9 -0.8 -0.8  0.7 -0.9
    cyl  -0.9  1.0  0.9  0.8 -0.7  0.8
    disp -0.8  0.9  1.0  0.8 -0.7  0.9
    hp   -0.8  0.8  0.8  1.0 -0.4  0.7
    drat  0.7 -0.7 -0.7 -0.4  1.0 -0.7
    wt   -0.9  0.8  0.9  0.7 -0.7  1.0
    
    #用ggcorrplot包提供的函数cor_pmat()计算p值
    p.mat <- cor_pmat(mtcars)
    head(p.mat[, 1:4])
    
    > head(p.mat[, 1:4])
                  mpg          cyl
    mpg  0.000000e+00 6.112687e-10
    cyl  6.112687e-10 0.000000e+00
    disp 9.380327e-10 1.802838e-12
    hp   1.787835e-07 3.477861e-09
    drat 1.776240e-05 8.244636e-06
    wt   1.293959e-10 1.217567e-07
                 disp           hp
    mpg  9.380327e-10 1.787835e-07
    cyl  1.802838e-12 3.477861e-09
    disp 0.000000e+00 7.142679e-08
    hp   7.142679e-08 0.000000e+00
    drat 5.282022e-06 9.988772e-03
    wt   1.222320e-11 4.145827e-05
    

    可视化相关性矩阵

    ggcorrplot(corr)#method默认为square,即方形
    
    cor_square
    # method = "circle" 圆形
    ggcorrplot(corr, method = "circle")
    
    cor_cicrl
    #重排矩阵,使用分层聚类
    ggcorrplot(corr, hc.order = TRUE, outline.color = "white")
    
    cor_hc
    #展示下半三角
    ggcorrplot(corr,
               hc.order = TRUE,
               type = "lower",
               outline.color = "white")
    
    cor_low
    #展示上半三角
    ggcorrplot(corr,
               hc.order = TRUE,
               type = "upper",
               outline.color = "white")
    
    cor_upper
    #改变主题和颜色
    ggcorrplot(
      corr,
      hc.order = TRUE,
      type = "lower",
      outline.color = "white",
      ggtheme = ggplot2::theme_gray,
      colors = c("#6D9EC1", "white", "#E46726")
    )
    
    cor_color
    #加上相关性系数标签
    ggcorrplot(corr,
               hc.order = TRUE,
               type = "lower",
               lab = TRUE)
    
    cor_lab
    #给有显著性差异的标上记
    ggcorrplot(corr,
               hc.order = TRUE,
               type = "lower",
               p.mat = p.mat)
    
    cor_pmat
    #在没有显著系数的地方留空
    ggcorrplot(
      corr,
      p.mat = p.mat,
      hc.order = TRUE,
      type = "lower",
      insig = "blank"
    )
    
    cor_pmat_blank

    参考

    https://rpkgs.datanovia.com/ggcorrplot/


    往期:

    ggpubr|让数据可视化更加优雅

    ggsci | 让你的配色Nature化

    相关文章

      网友评论

        本文标题:ggcorrplot | 简单的相关性热图绘制

        本文链接:https://www.haomeiwen.com/subject/pbctkltx.html