| Title: | Tidy Correlation Matrices and 'ggplot2' Correlograms |
|---|---|
| Description: | Computes correlation matrices as tidy data frames and creates publication-ready correlograms with 'ggplot2'. The package is designed for teaching and exploratory analysis workflows where users want one consistent interface for selecting numeric variables, calculating pairwise correlations, optionally estimating p-values, reordering variables, and drawing tile, point, or mixed correlograms. |
| Authors: | Xinyu Zhang [aut, cre] |
| Maintainer: | Xinyu Zhang <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-06-16 09:48:45 UTC |
| Source: | https://github.com/zhangx60/tidycorrgram |
Compute correlation matrices as tidy data frames and create publication-ready correlograms with ggplot2.
The main functions are corrgram_data for computing tidy
correlation tables and corrgram for drawing correlograms.
corrgram() computes correlations and returns a ggplot2 correlogram.
corrgram( data, columns = NULL, method = c("pearson", "kendall", "spearman"), use = "pairwise.complete.obs", triangle = c("lower", "full", "upper"), diagonal = TRUE, reorder = c("hclust", "none", "alphabetical"), p_values = FALSE, adjust = c("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr"), alpha = 0.05, exact = NULL, geom = c("tile", "point", "mixed"), labels = FALSE, label_digits = 2, palette = corrgram_palette(), significant_only = FALSE )corrgram( data, columns = NULL, method = c("pearson", "kendall", "spearman"), use = "pairwise.complete.obs", triangle = c("lower", "full", "upper"), diagonal = TRUE, reorder = c("hclust", "none", "alphabetical"), p_values = FALSE, adjust = c("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr"), alpha = 0.05, exact = NULL, geom = c("tile", "point", "mixed"), labels = FALSE, label_digits = 2, palette = corrgram_palette(), significant_only = FALSE )
data |
A data frame. |
columns |
Optional column selection. Use |
method |
Correlation method passed to |
use |
Missing-value handling passed to |
triangle |
Which part of the matrix to return: |
diagonal |
Should diagonal values be retained? |
reorder |
Variable ordering. |
p_values |
Should pairwise correlation p-values be computed? |
adjust |
P-value adjustment method passed to |
alpha |
Significance threshold used to create the |
exact |
Passed to |
geom |
Plot style. |
labels |
Should correlation values be drawn as text? |
label_digits |
Number of digits for text labels. |
palette |
A named color vector with |
significant_only |
If |
A ggplot2 object.
corrgram_data, corrgram_palette
corrgram(mtcars, columns = c("mpg", "disp", "hp", "wt")) corrgram(mtcars, geom = "point", triangle = "upper", diagonal = FALSE)corrgram(mtcars, columns = c("mpg", "disp", "hp", "wt")) corrgram(mtcars, geom = "point", triangle = "upper", diagonal = FALSE)
corrgram_data() selects numeric columns, computes pairwise correlations, and returns the matrix as a tidy data frame that is ready for ggplot2.
corrgram_data( data, columns = NULL, method = c("pearson", "kendall", "spearman"), use = "pairwise.complete.obs", triangle = c("full", "lower", "upper"), diagonal = TRUE, reorder = c("none", "hclust", "alphabetical"), p_values = FALSE, adjust = c("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr"), alpha = 0.05, exact = NULL )corrgram_data( data, columns = NULL, method = c("pearson", "kendall", "spearman"), use = "pairwise.complete.obs", triangle = c("full", "lower", "upper"), diagonal = TRUE, reorder = c("none", "hclust", "alphabetical"), p_values = FALSE, adjust = c("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr"), alpha = 0.05, exact = NULL )
data |
A data frame. |
columns |
Optional column selection. Use |
method |
Correlation method passed to |
use |
Missing-value handling passed to |
triangle |
Which part of the matrix to return: |
diagonal |
Should diagonal values be retained? |
reorder |
Variable ordering. |
p_values |
Should pairwise correlation p-values be computed? |
adjust |
P-value adjustment method passed to |
alpha |
Significance threshold used to create the |
exact |
Passed to |
A data frame with columns var1, var2, r, abs_r, row, and col. If p_values = TRUE, it also includes p, p_adjusted, and significant.
corrgram_data(mtcars, columns = c("mpg", "disp", "hp", "wt")) corrgram_data(mtcars, triangle = "lower", diagonal = FALSE)corrgram_data(mtcars, columns = c("mpg", "disp", "hp", "wt")) corrgram_data(mtcars, triangle = "lower", diagonal = FALSE)
Create a reusable three-color diverging palette for corrgram.
corrgram_palette(low = "#3B4CC0", mid = "#F7F7F7", high = "#B40426")corrgram_palette(low = "#3B4CC0", mid = "#F7F7F7", high = "#B40426")
low |
Color used for correlations near -1. |
mid |
Color used for correlations near 0. |
high |
Color used for correlations near 1. |
A named character vector with low, mid, and high.
corrgram_palette() corrgram_palette(low = "#2166AC", mid = "white", high = "#B2182B")corrgram_palette() corrgram_palette(low = "#2166AC", mid = "white", high = "#B2182B")