美文网首页那些年画图的简便操作R plotggplot2
使用corrplot包可视化相关性矩阵

使用corrplot包可视化相关性矩阵

作者: 生信摆渡 | 来源:发表于2021-01-21 08:23 被阅读0次

    介绍

    本文介绍如何绘制相关图中R.相关图相关矩阵的图形。突出显示数据表中最相关的变量非常有用。在该图中,相关系数根据该值着色。相关矩阵也可以根据变量之间的关联程度进行重新排序。这里使用R corrplot软件包。

    请注意,这里也可以使用在线软件来计算相关矩阵并绘制相关图,而无需进行任何安装。

    安装R corrplot软件包

    要执行本文中的R代码,需要corrplot程序包。

    install.packages("corrplot")
    

    相关分析数据

    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)
    head(round(M,2))
    
           mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
    mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
    cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
    disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
    hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
    drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09
    wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43
    

    相关图:可视化相关矩阵

    R corrplot函数用于绘制相关矩阵

    该函数的简化格式为:

    corrplot(corr, method="circle")
    
    参数 描述
    更正 相关矩阵可视化。要可视化常规矩阵,请使用is.corr = FALSE。
    方法 可视化方法:“圆圈”,“颜色”,“数字”等

    可视化方法

    可以使用七种不同的可视化方法:“圆形”,“正方形”,“椭圆”,“数字”,“阴影”,“颜色”,“饼图”( “circle”, “square”, “ellipse”, “number”, “shade”, “color”, “pie”)。

    library(corrplot)
    corrplot(M, method="circle")
    
    相关矩阵,R中的相关图,相关图
    corrplot(M, method="pie")
    
    相关矩阵,R中的相关图,相关图
    corrplot(M, method="color")
    
    相关矩阵,R中的相关图,相关图

    正相关以蓝色显示,负相关以红色显示。颜色强度和圆圈的大小与相关系数成正比。

    显示相关系数

    corrplot(M, method="number")
    
    相关矩阵,R中的相关图,相关图

    相关图布局的类型

    布局分为三种:

    • “ full”(默认):显示完整的相关矩阵
    • “ upper”:显示相关矩阵的上三角
    • “ lower”:显示相关矩阵的下三角
    corrplot(M, type="upper")
    
    相关矩阵,R中的相关图,相关图
    corrplot(M, type="lower")
    
    相关矩阵,R中的相关图,相关图

    重新排序相关矩阵

    相关矩阵可以根据被重新排序相关系数。这对于确定矩阵中隐藏的结构和图案很重要。在以下示例中,使用“ hclust”表示层次结构的聚类顺序。

    # correlogram with hclust reordering
    corrplot(M, type="upper", order="hclust")
    
    相关矩阵,R中的相关图,相关图
    # Using different color spectrum
    col<- colorRampPalette(c("red", "white", "blue"))(20)
    corrplot(M, type="upper", order="hclust", col=col)
    
    相关矩阵,R中的相关图,相关图
    # Change background color to lightblue
    corrplot(M, type="upper", order="hclust", col=c("black", "white"),
             bg="lightblue")
    
    相关矩阵,R中的相关图,相关图

    更改相关图的颜色

    如上节所示,可以自定义相关图的颜色。RcolorBrewer调色板的颜色在以下R脚本中使用:

    library(RColorBrewer)
    corrplot(M, type="upper", order="hclust", 
             col=brewer.pal(n=8, name="RdBu"))
    
    相关矩阵,R中的相关图,相关图
    corrplot(M, type="upper", order="hclust",
             col=brewer.pal(n=8, name="RdYlBu"))
    
    相关矩阵,R中的相关图,相关图
    corrplot(M, type="upper", order="hclust",
             col=brewer.pal(n=8, name="PuOr"))
    
    相关矩阵,R中的相关图,相关图

    更改文本标签的颜色和旋转

    tl.col(用于文本标签颜色)和 tl.srt(用于文本标签字符串旋转)用于更改文本的颜色和旋转。

    corrplot(M, type="upper", order="hclust", tl.col="black", tl.srt=45)
    
    相关矩阵,R中的相关图,相关图

    将相关图与显着性检验相结合

    计算相关性的p值

    为了计算p值矩阵,使用了一个自定义R函数:

    # mat : is a matrix of data
    # ... : further arguments to pass to the native R cor.test function
    cor.mtest <- function(mat, ...) {
        mat <- as.matrix(mat)
        n <- ncol(mat)
        p.mat<- matrix(NA, n, n)
        diag(p.mat) <- 0
        for (i in 1:(n - 1)) {
            for (j in (i + 1):n) {
                tmp <- cor.test(mat[, i], mat[, j], ...)
                p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
            }
        }
      colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
      p.mat
    }
    # matrix of the p-value of the correlation
    p.mat <- cor.mtest(mtcars)
    head(p.mat[, 1:5])
    
               mpg       cyl      disp        hp      drat
    mpg  0.000e+00 6.113e-10 9.380e-10 1.788e-07 1.776e-05
    cyl  6.113e-10 0.000e+00 1.803e-12 3.478e-09 8.245e-06
    disp 9.380e-10 1.803e-12 0.000e+00 7.143e-08 5.282e-06
    hp   1.788e-07 3.478e-09 7.143e-08 0.000e+00 9.989e-03
    drat 1.776e-05 8.245e-06 5.282e-06 9.989e-03 0.000e+00
    wt   1.294e-10 1.218e-07 1.222e-11 4.146e-05 4.784e-06
    

    向相关图添加显着性水平

    # Specialized the insignificant value according to the significant level
    corrplot(M, type="upper", order="hclust", 
             p.mat = p.mat, sig.level = 0.01)
    
    相关矩阵,R中的相关图,相关图
    # Leave blank on no significant coefficient
    corrplot(M, type="upper", order="hclust", 
             p.mat = p.mat, sig.level = 0.01, insig = "blank")
    

    另外,在上述图中,相关性p-值> 0.01被认为是微不足道的。在这种情况下,相关系数值留为空白或添加叉号。

    自定义相关图

    col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
    corrplot(M, method="color", col=col(200),  
             type="upper", order="hclust", 
             addCoef.col = "black", # Add coefficient of correlation
             tl.col="black", tl.srt=45, #Text label color and rotation
             # Combine with significance
             p.mat = p.mat, sig.level = 0.01, insig = "blank", 
             # hide correlation coefficient on the principal diagonal
             diag=FALSE 
             )
    
    相关矩阵,R中的相关图,相关图

    结论

    使用corrplot() R函数绘制相关矩阵的优美

    资讯

    参考corrplot介绍

    This analysis was performed using R (ver. 3.1.0).


    觉得有用的老铁麻烦点个小爱心~😏

    相关文章

      网友评论

        本文标题:使用corrplot包可视化相关性矩阵

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