The function ggstatsplot::ggcorrmat provides a quick way to produce publication-ready correlation matrix (aka correlalogram) plot. The function can also be used for quick data exploration. In addition to the plot, it can also be used to get a correlation coefficient matrix or the associated p-value matrix. Currently, the plot can display Pearson’s r, Spearman’s rho, and Kendall’s tau, and robust correlation coefficient (percentage bend correlation; see ?WRS2::pbcor). This function is a convenient wrapper around ggcorrplot::ggcorrplot function with some additional functionality.

We will see examples of how to use this function in this vignette with the gapminder and diamonds dataset.

To begin with, here are some instances where you would want to use ggcorrmat-

  • to easily visualize a correlation matrix using ggplot2
  • to quickly explore correlation between (all) numeric variables in the dataset

Note before: The following demo uses the pipe operator (%>%), so in case you are not familiar with this operator, here is a good explanation: http://r4ds.had.co.nz/pipes.html

Correlation matrix plot with ggcorrmat

For the first example, we will use the gapminder dataset (available in eponymous package on CRAN) provides values for life expectancy, Gross Domestic Product (GDP) per capita, and population, every five years, from 1952 to 2007, for each of 142 countries and was collected by the Gapminder Foundation. Let’s have a look at the data-

Let’s say we are interested in studying correlation between population of a country, average life expectancy, and GDP per capita across countries only for the year 2007.

The simplest way to get a correlation matrix is to stick to the defaults-

This plot can be further modified with additional arguments-

As seen from this correlation matrix, although there is no relationship between population and life expectancy worldwide, at least in 2007, there is a strong positive relationship between GDP, a well-established indicator of a country’s economic performance.

Given that there were only three variables, this doesn’t look that impressive. So let’s work with another example from ggplot2 package: the diamonds dataset. This dataset contains the prices and other attributes of almost 54,000 diamonds.

Let’s have a look at the data-

Let’s see the correlation matrix between different attributes of the diamond and the price.

We can make a number of changes to this basic correlation matrix. For example, since we were interested in relationship between price and other attributes, let’s make the price column to the the first column. Additionally, since we are running 6 correlations that are of a priori interest to us, we can adjust our threshold of significance to (0.05/6 ~ 0.008). Additionally, let’s use a non-parametric correlation coefficient. Please note that it is important to always make sure that the order in which cor.vars and cor.vars.names are entered is in sync. Otherwise, wrong column labels will be displayed.

As seen here, and unsurprisingly, the strongest predictor of the diamond price is its carat value, which a unit of mass equal to 200 mg. In other words, the heavier the diamond, the more expensive it is going to be.

Correlation statistics matrix with ggcorrmat

Another utility of ggcorrmat is in obtaining matrix of correlation coefficients and their p-values for a quick and dirty exploratory data analysis. For example, for the correlation matrix we just ran, we can get a coefficient matrix and a p-value matrix.

Note that if cor.vars are not specified, all numeric variables will be used. Moreover, you can also use abbreviations to specify what output you want in return.

# for reproducibility
set.seed(123)

# show four digits in a tibble
options(pillar.sigfig = 4)

# getting the correlation coefficient matrix
ggstatsplot::ggcorrmat(
  data = iris, # all numeric variables from data will be used
  corr.method = "np", # non-parametric
  return = "corr" # correlations
)
#> # A tibble: 4 x 5
#>   variable     Sepal.Length Sepal.Width Petal.Length Petal.Width
#>   <chr>               <dbl>       <dbl>        <dbl>       <dbl>
#> 1 Sepal.Length         1         -0.17          0.88       0.83 
#> 2 Sepal.Width         -0.17       1            -0.31      -0.290
#> 3 Petal.Length         0.88      -0.31          1          0.94 
#> 4 Petal.Width          0.83      -0.290         0.94       1

# getting the p-value matrix
ggstatsplot::ggcorrmat(
  data = ggplot2::msleep,
  cor.vars = sleep_total:bodywt,
  corr.method = "r", # robust
  return = "p", # p-values
  p.adjust.method = "holm"
)
#> # A tibble: 6 x 7
#>   variable  sleep_total sleep_rem sleep_cycle     awake   brainwt    bodywt
#>   <chr>           <dbl>     <dbl>       <dbl>     <dbl>     <dbl>     <dbl>
#> 1 sleep_to~   0.        5.291e-12   9.138e- 3 0.        3.170e- 5 2.568e- 6
#> 2 sleep_rem   4.070e-13 0.          1.978e- 2 5.291e-12 9.698e- 3 3.762e- 3
#> 3 sleep_cy~   2.285e- 3 1.978e- 2   0.        9.138e- 3 1.637e- 9 1.696e- 5
#> 4 awake       0.        4.070e-13   2.285e- 3 0.        3.170e- 5 2.568e- 6
#> 5 brainwt     4.528e- 6 4.849e- 3   1.488e-10 4.528e- 6 0.        4.509e-17
#> 6 bodywt      2.568e- 7 7.524e- 4   2.120e- 6 2.568e- 7 3.221e-18 0.

# getting the confidence intervals for correlations
ggstatsplot::ggcorrmat(
  data = ggplot2::msleep,
  cor.vars = sleep_total:bodywt,
  corr.method = "p", # parametric
  return = "ci", # confidence intervals
  p.adjust.method = "holm"
)
#> Note: In the correlation matrix,
#> the upper triangle: p-values adjusted for multiple comparisons
#> the lower triangle: unadjusted p-values.
#> # A tibble: 15 x 7
#>    pair                 r    lower    upper          p lower.adj  upper.adj
#>    <chr>            <dbl>    <dbl>    <dbl>      <dbl>     <dbl>      <dbl>
#>  1 sleep_total-s~  0.7518  0.6167   0.8438  2.916e- 12  0.5402     8.740e-1
#>  2 sleep_total-s~ -0.4737 -0.7058  -0.1498  6.169e-  3 -0.7738     7.215e-5
#>  3 sleep_total-a~ -1.0000 -1.0000  -1.0000  2.419e-226 -1.0000   -10.000e-1
#>  4 sleep_total-b~ -0.3605 -0.5694  -0.1078  6.348e-  3 -0.6290    -1.505e-2
#>  5 sleep_total-b~ -0.3120 -0.4944  -0.1033  4.085e-  3 -0.5302    -5.506e-2
#>  6 sleep_rem-sle~ -0.3381 -0.6144   0.01198 5.839e-  2 -0.6806     1.257e-1
#>  7 sleep_rem-awa~ -0.7518 -0.8438  -0.6167  2.911e- 12 -0.8748    -5.376e-1
#>  8 sleep_rem-bra~ -0.2213 -0.4756   0.06701 1.306e-  1 -0.4756     6.701e-2
#>  9 sleep_rem-bod~ -0.3277 -0.5353  -0.08265 9.947e-  3 -0.5838    -1.223e-2
#> 10 sleep_cycle-a~  0.4737  0.1498   0.7058  6.169e-  3 -0.006407   7.763e-1
#> 11 sleep_cycle-b~  0.8516  0.7088   0.9274  2.420e-  9  0.6080     9.487e-1
#> 12 sleep_cycle-b~  0.4178  0.08089  0.6690  1.734e-  2 -0.06265    7.410e-1
#> 13 awake-brainwt   0.3605  0.1078   0.5694  6.348e-  3  0.007931   6.333e-1
#> 14 awake-bodywt    0.3120  0.1032   0.4944  4.089e-  3  0.07202    5.178e-1
#> 15 brainwt-bodywt  0.9338  0.8892   0.9608  9.155e- 26  0.8583     9.697e-1

# getting the sample sizes for all pairs
ggstatsplot::ggcorrmat(
  data = ggplot2::msleep,
  cor.vars = sleep_total:bodywt,
  corr.method = "robust",
  return = "n" # note that n is different due to NAs
)
#> # A tibble: 6 x 7
#>   variable    sleep_total sleep_rem sleep_cycle awake brainwt bodywt
#>   <chr>             <dbl>     <dbl>       <dbl> <dbl>   <dbl>  <dbl>
#> 1 sleep_total          83        61          32    83      56     83
#> 2 sleep_rem            61        61          32    61      48     61
#> 3 sleep_cycle          32        32          32    32      30     32
#> 4 awake                83        61          32    83      56     83
#> 5 brainwt              56        48          30    56      56     56
#> 6 bodywt               83        61          32    83      56     83

Grouped analysis with grouped_ggcorrmat

What if we want to do the same analysis separately for each quality of the diamond cut (Fair, Good, Very Good, Premium, Ideal)?

ggstatsplot provides a special helper function for such instances: grouped_ggcorrmat. This is merely a wrapper function around ggstatsplot::combine_plots. It applies ggcorrmat across all levels of a specified grouping variable and then combines list of individual plots into a single plot. Note that the grouping variable can be anything: conditions in a given study, groups in a study sample, different studies, etc.

Note that this function also makes it easy to run the same correlation matrix across different levels of a factor/grouping variable. For example, if we wanted to get the same correlation coefficient matrix for color of the diamond, we can do the following-

As this example illustrates, there is a minimal coding overhead to explore correlations in your dataset with the grouped_ggcorrmat function.

Grouped analysis with ggcorrmat + purrr

Although grouped_ function is good for quickly exploring the data, it reduces the flexibility with which this function can be used. This is the because the common parameters used are applied to plots corresponding to all levels of the grouping variable and there is no way to customize the arguments for different levels of the grouping variable. We will see how this can be done using the purrr package.

See the associated vignette here: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/purrr_examples.html

Summary of tests

Following tests are carried out for each type of analyses-

Type Test
Parametric Pearson’s correlation coefficient
Non-parametric Spearman’s rank correlation coefficient
Robust Percentage bend correlation coefficient
Bayes Factor Pearson’s correlation coefficient

Suggestions

If you find any bugs or have any suggestions/remarks, please file an issue on GitHub: https://github.com/IndrajeetPatil/ggstatsplot/issues