美文网首页plot作图重点关注
利用ggstatsplot进行相关性分析并一步出图

利用ggstatsplot进行相关性分析并一步出图

作者: 欧阳松 | 来源:发表于2022-03-10 01:12 被阅读0次

    有人说ggstatsplot是统计学画图的天花板,确实他的统计异常简单,而且出的图也非常花里胡哨,这里介绍一下利用ggstatsplot相关性分析
    使用mtcars数据进行演示

    data(mtcars)
    mtcars[1:10,1:10] ##显示前十行前十列的数据
    
    mpg cyl disp hp drat wt qsec vs am gear
    Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4
    Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4
    Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4
    Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3
    Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3
    Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3
    Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3
    Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4
    Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4
    Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4

    进行相关性分析,需要用到的函数是ggcorrmat(),我们先看看官方介绍

    ggcorrmat(
      data,
      cor.vars = NULL,
      cor.vars.names = NULL,
      output = "plot",
      matrix.type = "upper",
      type = "parametric",
      tr = 0.2,
      partial = FALSE,
      k = 2L,
      sig.level = 0.05,
      conf.level = 0.95,
      bf.prior = 0.707,
      p.adjust.method = "holm",
      pch = "cross",
      ggcorrplot.args = list(method = "square", outline.color = "black", pch.cex = 14),
      package = "RColorBrewer",
      palette = "Dark2",
      colors = c("#E69F00", "white", "#009E73"),
      ggtheme = ggstatsplot::theme_ggstatsplot(),
      ggplot.component = NULL,
      title = NULL,
      subtitle = NULL,
      caption = NULL,
      ...
    )
    

    可以看到可以定制的参数很多,这样我们就可以一一去尝试了

    我们首先运行最简单的默认函数,见Figure 1所示,可以看到默认的是上半部分显示,并且自动计算好了相关性系数和显著性标识,非常的方便

    library(ggstatsplot)
    ggcorrmat(mtcars)
    
    默认函数的图
    这时候我们可以跟之前的结果比较,会发现P值显示不一样,那是因为默认的是校正的p值,如果我们只想显示默认p值,只要设置p.adjust.method = 'none'即可,同时我们再设置成显示下半部分,只需要加一句matrix.type = 'lower',结果见Figure 2
    ggcorrmat(mtcars,p.adjust.method = 'none',matrix.type = 'lower')
    
    将p值设置为none,即不校正

    由于所有计算都是后台运行,如果我们想看结果的话,只需要设置output = "dataframe"即可,这里我们依然可以用校正p值,或者不校正p值,我们试试不校正p值,结果见表所示

    data<-ggcorrmat(mtcars,p.adjust.method = 'none',output = 'datafream')
    data[1:10,1:11]
    
    parameter1 parameter2 estimate conf.level conf.low conf.high statistic df.error p.value method n.obs
    mpg cyl -0.8521620 0.95 -0.9257694 -0.7163171 -8.919699 30 0.0000000 Pearson correlation 32
    mpg disp -0.8475514 0.95 -0.9233594 -0.7081376 -8.747151 30 0.0000000 Pearson correlation 32
    mpg hp -0.7761684 0.95 -0.8852686 -0.5860994 -6.742388 30 0.0000002 Pearson correlation 32
    mpg drat 0.6811719 0.95 0.4360484 0.8322010 5.096042 30 0.0000178 Pearson correlation 32
    mpg wt -0.8676594 0.95 -0.9338264 -0.7440872 -9.559044 30 0.0000000 Pearson correlation 32
    mpg qsec 0.4186840 0.95 0.0819549 0.6696186 2.525213 30 0.0170820 Pearson correlation 32
    mpg vs 0.6640389 0.95 0.4103630 0.8223262 4.864385 30 0.0000342 Pearson correlation 32
    mpg am 0.5998324 0.95 0.3175583 0.7844520 4.106127 30 0.0002850 Pearson correlation 32
    mpg gear 0.4802848 0.95 0.1580618 0.7100628 2.999191 30 0.0054009 Pearson correlation 32
    mpg carb -0.5509251 0.95 -0.7546480 -0.2503183 -3.615750 30 0.0010844 Pearson correlation 32

    接下来,我们可以深入定制

    全部显示结果,见Figure 3所示

    ggcorrmat(mtcars, ##表达矩阵
              p.adjust.method = 'none', ## 不校正p值
              matrix.type = 'full'##全部显示
              )
    
    显示全部
    如果觉得配色不好,我们还可以设置不同的颜色差,这里要有三组颜色,比如我们设置蓝色白色和红色,结果见Figure 5所示
    ggcorrmat(mtcars,p.adjust.method = 'none',
                      colors=c("blue" ,"white" ,"red"))
    
    更改颜色

    当然我们还可以自定义别的颜色,比如下面这个素颜的配色

    ggcorrmat(mtcars,p.adjust.method = 'none',colors=c("#2874C5" ,"white" ,"#f87669"))
    
    素雅的图

    相关文章

      网友评论

        本文标题:利用ggstatsplot进行相关性分析并一步出图

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