Skip to contents

Computes a correlation matrix and corresponding p-values, and visualises the lower triangle as an interactive heatmap using plotly. Correlation values and significance stars can be displayed inside each tile.

Usage

plot_corr_matrix(
  data,
  method = c("pearson", "spearman"),
  use = c("pairwise.complete.obs", "complete.obs", "everything", "all.obs",
    "na.or.complete"),
  digits = 2,
  show_stars = TRUE,
  show_values = TRUE,
  colors = c("#11A7F2", "#FFFFFF", "#E00087"),
  ...
)

Arguments

data

A data frame or matrix containing only numeric variables.

method

Correlation method: "pearson" or "spearman", passed directly to stats::cor() and stats::cor.test().

use

Missing-data handling passed directly to stats::cor(). One of "pairwise.complete.obs", "complete.obs", "everything", "all.obs", or "na.or.complete".

digits

Number of decimal places for correlation labels.

show_stars

Logical; display significance stars (*, **, ***) based on p-values.

show_values

Logical; display correlation values inside tiles.

colors

A vector of colours for the heatmap colour scale. Defaults to NPDA colours.

...

Additional arguments passed to plotly::plot_ly(). These can override default plotly arguments such as showscale, or colorbar.

Value

A plotly heatmap object.

Examples

# Basic correlation matrix
plot_corr_matrix(mtcars)
# Spearman correlation without tile labels, with additional plotly arguments plot_corr_matrix(mtcars, method = "spearman", show_values = FALSE, show_stars = TRUE, colorbar = list(title = "Correlation")) |> plotly::layout(annotations = list( text = paste0("<i><b>Note:</b>", "*: p<0.05, **: p<0.01, ***: p<0.001.</i>"), x = 1, y = 1, xref = "paper", yref = "paper", xanchor = "right", yanchor = "top", showarrow = FALSE))