Function to plot either a) the upper triangle component of a correlation matrix, or b) a panel of correlations (e.g., between raw and component scores from a PCA, between predictors and different outcomes, etc.).

plot_correlations(
  dtf = NULL,
  R = NULL,
  n = NULL,
  p_values = NULL,
  labels = NULL,
  label_pos = c(0.2, 0.25),
  only_upper_tri = TRUE,
  new = TRUE,
  width = 5,
  height = 5,
  margin = NULL,
  fill = c("#E69F00", "#56B4E9"),
  opaque = 0.4,
  cex = c(0.8, 0.8),
  value = TRUE,
  digits = 2,
  method = "BH",
  alpha = 0.05,
  legend_pos = c(0, 0.5, 0)
)

Arguments

dtf

A data frame, a set of variables to compute a correlation matrix over.

R

A matrix of correlations (must be supplied if dtf is not provided, otherwise computed automatically).

n

An integer, the sample size for the correlations (computed automatically if dtf is provided).

p_values

A matrix of p-values (must have same dimensions as R).

labels

Either a character vector with the labels for the rows of the correlation matrix, or a list of character vectors with the labels for the rows and columns, respectively, for the correlation matrix.

label_pos

A numeric vector the x-axis adjustment for row labels and the y-axis adjustment for column labels, respectively.

only_upper_tri

A logical value, TRUE if only the upper triangle of the correlation matrix should be plotted.

new

A logical value; if TRUE a new plotting window is created.

width

An integer value, the width in inches of the plot.

height

An integer value, the height in inches of the plot.

margin

A numeric vector, four values specifying the margins of the plot in inches, giving the spacing for the bottom, left, top, and right sides respectively.

fill

A character vector, the colors for negative and positive correlations respectively.

opaque

A numeric value ranging from 0 to 1, with lower values indicating greater translucency of the fill color.

cex

A numeric vector of two values, the text size for the row/column labels and correlation values, respectively.

digits

An integer value, the number of digits to round correlation values to.

method

A character string, the method to use for multiple comparison adjustment (see stats::p.adjust).

alpha

A numeric value between 0 and 1, the cut-off for statistical significance (default is 0.05).

legend_pos

A numeric vector of 3 values governing the y-axis position and x-axis positions, respectively, for the legend.

Value

A plot of the correlations.

Examples

# Correlation matrix
data(mtcars)
plot_correlations( dtf = mtcars, new = FALSE )


# Example based on PCA

# Loading matrix
lambda <- cbind(
  c( runif( 4, .3, .9 ), rep( 0, 4 ) ),
  c( rep( 0, 4 ), runif( 4, .3, .9 ) )
)
# Communalities
D_tau <- diag( runif( 8, .5, 1.5 ) )

cov_mat <- lambda %*% t( lambda ) + D_tau
cor_mat <- cov2cor( cov_mat )

set.seed( 341 ) # For reproducibility
x <- MASS::mvrnorm( n = 200, mu = rep( 0, 8 ), Sigma = cor_mat )
colnames(x) <- paste0( 'C', 1:8 )
PCA <- principal_components_analysis( x )

# Correlations between raw variables
# and component scores from PCA
plot_correlations(
  R = PCA$Correlations$Train[, 1:2], n = 200,
  labels = list(
    paste0( 'Variable ', 1:8 ),
    paste0( 'Comp. ', 1:2 )
  ),
  only_upper_tri = F, margin = c( .25, 2, .25, .25 ),
  legend_pos = c( 0, 0, -.25 ),
  new = FALSE
)