Setup

Correlation Matrix

(related function documentation)

The default output for correlation() is a detailed overview including test statistic, p-values and confidence intervals. A shorter summary in matrix-layout can be obtained by using summary().

result <- correlation(iris)

result
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter1   |   Parameter2 |     r |         95% CI | t(148) |         p
#> -------------------------------------------------------------------------
#> Sepal.Length |  Sepal.Width | -0.12 | [-0.27,  0.04] |  -1.44 | 0.152    
#> Sepal.Length | Petal.Length |  0.87 | [ 0.83,  0.91] |  21.65 | < .001***
#> Sepal.Length |  Petal.Width |  0.82 | [ 0.76,  0.86] |  17.30 | < .001***
#> Sepal.Width  | Petal.Length | -0.43 | [-0.55, -0.29] |  -5.77 | < .001***
#> Sepal.Width  |  Petal.Width | -0.37 | [-0.50, -0.22] |  -4.79 | < .001***
#> Petal.Length |  Petal.Width |  0.96 | [ 0.95,  0.97] |  43.39 | < .001***
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 150

summary(result)
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter    | Petal.Width | Petal.Length | Sepal.Width
#> -------------------------------------------------------
#> Sepal.Length |     0.82*** |      0.87*** |       -0.12
#> Sepal.Width  |    -0.37*** |     -0.43*** |            
#> Petal.Length |     0.96*** |              |            
#> 
#> p-value adjustment method: Holm (1979)

The result from summary() can be used to create a plot.

s <- summary(result)
plot(s, size_point = 2)

To change the style of geoms, use the type-argument.

plot(s, type = "tile")

It is also possible to add correlation coefficients and p-values.

plot(s, show_values = TRUE, show_p = TRUE, show_legend = FALSE)

And a “redundant” summary can be plotted as well:

s <- summary(result, redundant = TRUE)

s
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter    | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width
#> ----------------------------------------------------------------------
#> Sepal.Length |      1.00*** |       -0.12 |      0.87*** |     0.82***
#> Sepal.Width  |        -0.12 |     1.00*** |     -0.43*** |    -0.37***
#> Petal.Length |      0.87*** |    -0.43*** |      1.00*** |     0.96***
#> Petal.Width  |      0.82*** |    -0.37*** |      0.96*** |     1.00***
#> 
#> p-value adjustment method: Holm (1979)

plot(s)

For larger correlation matrices, the size_point argument can be used to adjust size of point geoms.

result <- correlation(mtcars[, -c(8:9)])
result
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter1 | Parameter2 |     r |         95% CI | t(30) |         p
#> --------------------------------------------------------------------
#> mpg        |        cyl | -0.85 | [-0.93, -0.72] | -8.92 | < .001***
#> mpg        |       disp | -0.85 | [-0.92, -0.71] | -8.75 | < .001***
#> mpg        |         hp | -0.78 | [-0.89, -0.59] | -6.74 | < .001***
#> mpg        |       drat |  0.68 | [ 0.44,  0.83] |  5.10 | < .001***
#> mpg        |         wt | -0.87 | [-0.93, -0.74] | -9.56 | < .001***
#> mpg        |       qsec |  0.42 | [ 0.08,  0.67] |  2.53 | 0.137    
#> mpg        |       gear |  0.48 | [ 0.16,  0.71] |  3.00 | 0.065    
#> mpg        |       carb | -0.55 | [-0.75, -0.25] | -3.62 | < .05*   
#> cyl        |       disp |  0.90 | [ 0.81,  0.95] | 11.45 | < .001***
#> cyl        |         hp |  0.83 | [ 0.68,  0.92] |  8.23 | < .001***
#> cyl        |       drat | -0.70 | [-0.84, -0.46] | -5.37 | < .001***
#> cyl        |         wt |  0.78 | [ 0.60,  0.89] |  6.88 | < .001***
#> cyl        |       qsec | -0.59 | [-0.78, -0.31] | -4.02 | < .01**  
#> cyl        |       gear | -0.49 | [-0.72, -0.17] | -3.10 | 0.054    
#> cyl        |       carb |  0.53 | [ 0.22,  0.74] |  3.40 | < .05*   
#> disp       |         hp |  0.79 | [ 0.61,  0.89] |  7.08 | < .001***
#> disp       |       drat | -0.71 | [-0.85, -0.48] | -5.53 | < .001***
#> disp       |         wt |  0.89 | [ 0.78,  0.94] | 10.58 | < .001***
#> disp       |       qsec | -0.43 | [-0.68, -0.10] | -2.64 | 0.131    
#> disp       |       gear | -0.56 | [-0.76, -0.26] | -3.66 | < .05*   
#> disp       |       carb |  0.39 | [ 0.05,  0.65] |  2.35 | 0.177    
#> hp         |       drat | -0.45 | [-0.69, -0.12] | -2.75 | 0.110    
#> hp         |         wt |  0.66 | [ 0.40,  0.82] |  4.80 | < .001***
#> hp         |       qsec | -0.71 | [-0.85, -0.48] | -5.49 | < .001***
#> hp         |       gear | -0.13 | [-0.45,  0.23] | -0.69 | > .999   
#> hp         |       carb |  0.75 | [ 0.54,  0.87] |  6.21 | < .001***
#> drat       |         wt | -0.71 | [-0.85, -0.48] | -5.56 | < .001***
#> drat       |       qsec |  0.09 | [-0.27,  0.43] |  0.50 | > .999   
#> drat       |       gear |  0.70 | [ 0.46,  0.84] |  5.36 | < .001***
#> drat       |       carb | -0.09 | [-0.43,  0.27] | -0.50 | > .999   
#> wt         |       qsec | -0.17 | [-0.49,  0.19] | -0.97 | > .999   
#> wt         |       gear | -0.58 | [-0.77, -0.29] | -3.93 | < .01**  
#> wt         |       carb |  0.43 | [ 0.09,  0.68] |  2.59 | 0.132    
#> qsec       |       gear | -0.21 | [-0.52,  0.15] | -1.19 | > .999   
#> qsec       |       carb | -0.66 | [-0.82, -0.40] | -4.76 | < .001***
#> gear       |       carb |  0.27 | [-0.08,  0.57] |  1.56 | 0.774    
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 32

s <- summary(result)
plot(s, size_point = .8)

Gaussian Graphical Models (GGMs)

To create a Gaussian Graphical Models plot, the library ggraph needs to be loaded first.

library(ggraph)
result <- correlation(mtcars, partial = TRUE)

result
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter1 | Parameter2 |     r |         95% CI | t(30) |         p
#> --------------------------------------------------------------------
#> mpg        |        cyl | -0.02 | [-0.37,  0.33] | -0.13 | > .999   
#> mpg        |       disp |  0.16 | [-0.20,  0.48] |  0.89 | > .999   
#> mpg        |         hp | -0.21 | [-0.52,  0.15] | -1.18 | > .999   
#> mpg        |       drat |  0.10 | [-0.25,  0.44] |  0.58 | > .999   
#> mpg        |         wt | -0.39 | [-0.65, -0.05] | -2.34 | > .999   
#> mpg        |       qsec |  0.24 | [-0.12,  0.54] |  1.34 | > .999   
#> mpg        |         vs |  0.03 | [-0.32,  0.38] |  0.18 | > .999   
#> mpg        |         am |  0.26 | [-0.10,  0.56] |  1.46 | > .999   
#> mpg        |       gear |  0.10 | [-0.26,  0.43] |  0.52 | > .999   
#> mpg        |       carb | -0.05 | [-0.39,  0.30] | -0.29 | > .999   
#> cyl        |       disp |  0.27 | [-0.09,  0.56] |  1.51 | > .999   
#> cyl        |         hp |  0.17 | [-0.19,  0.49] |  0.97 | > .999   
#> cyl        |       drat | -0.27 | [-0.57,  0.09] | -1.55 | > .999   
#> cyl        |         wt | -0.11 | [-0.44,  0.25] | -0.62 | > .999   
#> cyl        |       qsec | -0.26 | [-0.56,  0.10] | -1.48 | > .999   
#> cyl        |         vs | -0.32 | [-0.60,  0.03] | -1.85 | > .999   
#> cyl        |         am | -0.25 | [-0.55,  0.11] | -1.39 | > .999   
#> cyl        |       gear | -0.35 | [-0.62,  0.00] | -2.04 | > .999   
#> cyl        |       carb |  0.23 | [-0.13,  0.53] |  1.27 | > .999   
#> disp       |         hp |  0.54 | [ 0.24,  0.75] |  3.52 | 0.072    
#> disp       |       drat |  0.10 | [-0.26,  0.43] |  0.53 | > .999   
#> disp       |         wt |  0.76 | [ 0.56,  0.88] |  6.43 | < .001***
#> disp       |       qsec | -0.31 | [-0.60,  0.04] | -1.80 | > .999   
#> disp       |         vs | -0.10 | [-0.44,  0.25] | -0.57 | > .999   
#> disp       |         am | -0.07 | [-0.41,  0.29] | -0.37 | > .999   
#> disp       |       gear |  0.06 | [-0.29,  0.40] |  0.34 | > .999   
#> disp       |       carb | -0.66 | [-0.82, -0.40] | -4.77 | < .01**  
#> hp         |       drat | -0.06 | [-0.40,  0.29] | -0.34 | > .999   
#> hp         |         wt | -0.30 | [-0.59,  0.06] | -1.72 | > .999   
#> hp         |       qsec | -0.05 | [-0.39,  0.30] | -0.29 | > .999   
#> hp         |         vs |  0.27 | [-0.08,  0.57] |  1.55 | > .999   
#> hp         |         am |  0.10 | [-0.26,  0.43] |  0.55 | > .999   
#> hp         |       gear |  0.11 | [-0.25,  0.44] |  0.59 | > .999   
#> hp         |       carb |  0.50 | [ 0.19,  0.72] |  3.19 | 0.167    
#> drat       |         wt | -0.11 | [-0.44,  0.25] | -0.62 | > .999   
#> drat       |       qsec | -0.06 | [-0.40,  0.29] | -0.33 | > .999   
#> drat       |         vs |  0.03 | [-0.33,  0.37] |  0.14 | > .999   
#> drat       |         am |  0.12 | [-0.24,  0.45] |  0.68 | > .999   
#> drat       |       gear |  0.06 | [-0.29,  0.40] |  0.35 | > .999   
#> drat       |       carb |  0.21 | [-0.15,  0.52] |  1.18 | > .999   
#> wt         |       qsec |  0.55 | [ 0.24,  0.75] |  3.57 | 0.063    
#> wt         |         vs | -0.06 | [-0.40,  0.29] | -0.36 | > .999   
#> wt         |         am |  0.02 | [-0.33,  0.36] |  0.10 | > .999   
#> wt         |       gear | -0.13 | [-0.46,  0.23] | -0.71 | > .999   
#> wt         |       carb |  0.62 | [ 0.34,  0.80] |  4.30 | < .01**  
#> qsec       |         vs |  0.35 | [ 0.00,  0.62] |  2.02 | > .999   
#> qsec       |         am | -0.32 | [-0.60,  0.03] | -1.84 | > .999   
#> qsec       |       gear | -0.10 | [-0.43,  0.26] | -0.56 | > .999   
#> qsec       |       carb | -0.25 | [-0.55,  0.11] | -1.40 | > .999   
#> vs         |         am | -0.21 | [-0.52,  0.15] | -1.18 | > .999   
#> vs         |       gear |  0.04 | [-0.31,  0.38] |  0.22 | > .999   
#> vs         |       carb | -0.09 | [-0.43,  0.27] | -0.50 | > .999   
#> am         |       gear |  0.28 | [-0.08,  0.57] |  1.58 | > .999   
#> am         |       carb | -0.05 | [-0.39,  0.31] | -0.25 | > .999   
#> gear       |       carb |  0.43 | [ 0.09,  0.68] |  2.59 | 0.727    
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 32

plot(result)