library(dplyr) library(corrr) knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

corrr is a package for exploring **corr**elations in **R**. It makes it possible to easily perform routine tasks when exploring correlation matrices such as ignoring the diagonal, focusing on the correlations of certain variables against others, or rearranging and visualizing the matrix in terms of the strength of the correlations.

Using `corrr`

starts with `correlate()`

, which acts like the base correlation function `cor()`

. It differs by defaulting to pairwise deletion, and returning a correlation data frame (`cor_df`

) of the following structure:

- A
`tbl`

with an additional class,`cor_df`

- An extra "term" column
- Standardized variances (the matrix diagonal) set to missing values (
`NA`

) so they can be ignored.

To work with further, let's create a correlation data frame using `correlate()`

from the `mtcars`

data that comes with R:

library(corrr) d <- correlate(mtcars, quiet = TRUE) d

At first, a correlation data frame might seem like an unnecessary complexity compared to the traditional matrix. However, the purpose of corrr is to help use explore these correlations, not to do mathematical or statistical operations. Thus, by having the correlations in a data frame, we can make use of packages that help us work with data frames like `dplyr`

, `tidyr`

, `ggplot2`

, and focus on using data pipelines. Lets look at some examples:

library(dplyr) # Filter rows to occasions in which cyl has a correlation of .7 or more with # another variable. d %>% filter(cyl > .7) # Select the mpg, cyl and disp columns (and term) d %>% select(term, mpg, cyl, disp) # Combine above in a single pipeline d %>% filter(cyl > .7) %>% select(term, mpg, cyl, disp)

Furthermore, by having the diagonal set to missing, we don't need to put in extra effort to ignore them when summarizing the correlations. For example:

# Compute mean of each column library(purrr) d %>% select(-term) %>% map_dbl(~ mean(., na.rm = TRUE))

As the above section suggests, the corrr API is designed with data pipelines in mind (e.g., to use `%>%`

from the magrittr package). After `correlate()`

, the primary corrr functions take a `cor_df`

as their first argument, and return a `cor_df`

or `tbl`

(or output like a plot). These functions serve one of three purposes:

Internal changes (`cor_df`

out):

`shave()`

the upper or lower triangle (set to`r NA`

).`rearrange()`

the columns and rows based on correlation strengths.

Reshape structure (`tbl`

or `cor_df`

out):

`focus()`

on select columns and rows.`stretch()`

into a long format.

Output/visualizations (console/plot out):

`fashion()`

the correlations for pretty printing.`rplot()`

a shape for each correlation.`network_plot()`

a point for each variable, joined by paths for correlations.

By combing these functions in data pipelines, it's possible to easily explore your correlations.

For example, lets focus on the correlations of mpg and cyl with all the others:

d %>% focus(mpg, cyl)

Or maybe we want to focus in on a few variables (mirrored in rows too) and print the correlations without an upper triangle and fashioned to look nice:

d %>% focus(mpg:drat, mirror = TRUE) %>% # Focus only on mpg:drat shave() %>% # Remove the upper triangle fashion() # Print in nice format

Alternatively, we can visualize these correlations (let's clear the lower triangle for a change):

d %>% focus(mpg:drat, mirror = TRUE) %>% shave(upper = FALSE) %>% rplot() # Plot

Perhaps we'd like to rearrange the correlations so that the plot becomes easier to interpret. In this case, we can add `rearrange()`

into our pipeline before shaving one of the triangles (we'll take correlation sign into account with `absolute = FALSE`

).

d %>% focus(mpg:drat, mirror = TRUE) %>% rearrange(absolute = FALSE) %>% shave() %>% rplot()

For other resources about how to use `corrr`

, you'll find plenty of posts explaining functions at blogR, or keep up to date with these on Twitter by following \@drsimonj.

**Any scripts or data that you put into this service are public.**

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.