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.
Arguments
- data
A data frame or matrix containing only numeric variables.
- method
Correlation method:
"pearson"or"spearman", passed directly tostats::cor()andstats::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 asshowscale, orcolorbar.
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))