| Type: | Package |
| Title: | Visualization of a Correlation Matrix using 'ggplot2' |
| Version: | 0.3.0 |
| Date: | 2026-07-24 |
| Description: | The 'ggcorrplot' package can be used to visualize easily a correlation matrix using 'ggplot2'. It provides a solution for reordering the correlation matrix and displays the significance level on the plot. It also includes a function for computing a matrix of correlation p-values. |
| License: | GPL-2 |
| URL: | https://rpkgs.datanovia.com/ggcorrplot/, https://github.com/kassambara/ggcorrplot |
| BugReports: | https://github.com/kassambara/ggcorrplot/issues |
| Depends: | R (≥ 3.3), ggplot2 (≥ 3.3.6) |
| Imports: | grDevices, reshape2, stats |
| Suggests: | testthat (≥ 3.0.0), knitr, rmarkdown, spelling, vdiffr (≥ 1.0.0) |
| VignetteBuilder: | knitr |
| Encoding: | UTF-8 |
| Language: | en-US |
| Config/testthat/edition: | 3 |
| Config/roxygen2/version: | 8.0.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-07-24 19:28:17 UTC; kassambara |
| Author: | Alboukadel Kassambara [aut, cre],
Indrajeet Patil |
| Maintainer: | Alboukadel Kassambara <alboukadel.kassambara@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-07-24 20:50:02 UTC |
Visualization of a correlation matrix using ggplot2
Description
ggcorrplot(): A graphical display of a correlation matrix using ggplot2. The main plot options are walked through in ggcorrplot: Correlation Matrix Heatmap in R with ggplot2.
cor_pmat(): Compute a correlation matrix p-values, to mark the significant cells. A worked example is in ggcorrplot: Correlation Matrix Heatmap in R with ggplot2.
Usage
ggcorrplot(
corr,
method = c("square", "circle"),
type = c("full", "lower", "upper"),
ggtheme = ggplot2::theme_minimal,
title = "",
show.legend = TRUE,
legend.title = "Corr",
show.diag = NULL,
colors = c("blue", "white", "red"),
outline.color = "gray",
hc.order = FALSE,
hc.method = "complete",
lab = FALSE,
lab_col = "black",
lab_size = 4,
lab_fontface = "plain",
sig.stars = FALSE,
p.mat = NULL,
sig.level = 0.05,
insig = c("pch", "blank", "stars"),
pch = 4,
pch.col = "black",
pch.cex = 5,
tl.cex = 12,
tl.col = NULL,
tl.srt = 45,
tl.vjust = 1,
tl.hjust = 1,
digits = 2,
as.is = FALSE,
nsmall = 0L,
leading.zero = TRUE,
legend.limit = c(-1, 1),
circle.scale = 1,
coord.fixed = TRUE,
lower.method = NULL,
upper.method = NULL,
hc.rect = NULL,
palette = NULL,
preset = NULL,
hc.rect.col = "gray30",
scale.square = FALSE,
cell.grid = FALSE,
cell.grid.col = "grey90"
)
cor_pmat(x, ..., use = c("pairwise.complete.obs", "everything"))
Arguments
corr |
the correlation matrix to visualize |
method |
character, the visualization method of correlation matrix to be used. Allowed values are "square" (default), "circle". |
type |
character, "full" (default), "lower" or "upper" display. A mixed
layout (see |
ggtheme |
ggplot2 function or theme object. Default value is
'theme_minimal'. Allowed values are the official ggplot2 themes including
theme_gray, theme_bw, theme_minimal, theme_classic, theme_void, .... Theme
objects are also allowed (e.g., 'theme_classic()'). A mixed "number" region
reads the background from this argument, resolved against the default theme
in force when |
title |
character, title of the graph. |
show.legend |
logical, if TRUE the legend is displayed. |
legend.title |
a character string for the legend title. lower triangular, upper triangular or full matrix. |
show.diag |
NULL or logical, whether display the correlation
coefficients on the principal diagonal. If |
colors |
a vector of colors for the fill gradient. The default is a
length-3 vector for the low, mid and high correlation values (mapped with
|
outline.color |
the outline color of square or circle. Default value is "gray". |
hc.order |
logical value. If TRUE, correlation matrix will be hc.ordered using hclust function. |
hc.method |
the agglomeration method to be used in hclust (see ?hclust). |
lab |
logical value. If TRUE, add correlation coefficient on the plot. |
lab_col, lab_size |
size and color to be used for the correlation coefficient labels. used when lab = TRUE. |
lab_fontface |
the font face ( |
sig.stars |
logical value. If |
p.mat |
matrix of p-value. If NULL, arguments sig.level, insig, pch, pch.col, pch.cex is invalid. |
sig.level |
significant level, if the p-value in p-mat is bigger than
sig.level, then the corresponding correlation coefficient is regarded as
insignificant. This governs which cells |
insig |
character, how to convey significance from |
pch |
add character on the glyphs of insignificant correlation coefficients (only valid when insig is "pch"). Default value is 4. |
pch.col, pch.cex |
the color and the cex (size) of pch (only valid when insig is "pch"). |
tl.cex, tl.col, tl.srt |
the size, the color and the string rotation of
text label (variable names). |
tl.vjust, tl.hjust |
the vertical and horizontal justification of the
x-axis text labels, passed to |
digits |
Decides the number of decimal digits to be displayed (Default: '2'). |
as.is |
retained for backward compatibility; no longer affects the plot. The axis is now always drawn in the matrix (row/column) order, so the variable-name handling this argument used to control is done internally. |
nsmall |
the minimum number of digits to the right of the decimal point
in the coefficient labels, passed to |
leading.zero |
logical. If |
legend.limit |
a length-2 numeric vector giving the limits of the fill
color scale. Default |
circle.scale |
a scaling factor for the circle sizes when
|
coord.fixed |
logical value. If |
lower.method, upper.method |
character, an optional per-triangle glyph for
a mixed layout: one of "square", "circle" or "number" (the coefficient drawn
as text, colored by its value on the fill ramp). When either is set, the plot
switches to a mixed layout where the lower and upper triangles are drawn
separately and the variable names are drawn on the diagonal; a triangle left
Over a light background the "number" text is drawn on a darkened copy of the
ramp – same hues, so warm still reads as positive and cool as negative, but
dark enough that a coefficient near zero stays readable instead of washing
out. Over a dark background the ramp is used as given, its pale middle being
what reads there. The background is taken from |
hc.rect |
integer or |
palette |
optional name of a built-in colorblind-safe diverging palette
for the fill gradient: |
preset |
optional name of a bundle of publication-grade defaults. The only
value, |
hc.rect.col |
the outline color of the |
scale.square |
logical. If |
cell.grid |
logical. If |
cell.grid.col |
the color of the |
x |
numeric matrix or data frame |
... |
other arguments to be passed to the function cor.test. |
use |
character, how to treat pairs involving missing values when
deciding which cells are |
Details
cor_pmat() tests each pair of columns with
cor.test. A pair with fewer than three overlapping
non-missing observations (which cor.test cannot test,
e.g. two variables that never co-occur) yields NA for that cell
rather than aborting the whole computation. Pairs that can be tested are
computed as before, and errors they raise are passed through.
The use argument controls which pairs are returned as NA so
the p-value matrix can be aligned with a correlation matrix built the same
way. With the default "pairwise.complete.obs" every pair that has
enough overlapping observations is tested (the previous behavior). With
"everything" a pair is set to NA whenever either variable has
any missing value, so the NA pattern matches
cor(x) with its default use = "everything".
Value
ggcorrplot(): Returns a ggplot2
cor_pmat(): Returns a matrix containing the p-values of correlations
See Also
ggcorrplot: Correlation Matrix Heatmap in R with ggplot2 for worked examples of the plot, and Correlation Matrix in R: Compute, Visualize & P-values for computing the matrix and its p-values beforehand.
Correlation Test in R: Pearson, Spearman & Kendall for the test behind the p-values.
Examples
# Compute a correlation matrix
data(mtcars)
corr <- round(cor(mtcars), 1)
corr
# Compute a matrix of correlation p-values
p.mat <- cor_pmat(mtcars)
p.mat
# Visualize the correlation matrix
# --------------------------------
# method = "square" or "circle"
ggcorrplot(corr)
ggcorrplot(corr, method = "circle")
# Mixed layout: a different glyph per triangle
# --------------------------------
# numbers in the lower triangle, circles in the upper, names on the diagonal
ggcorrplot(corr,
lower.method = "number", upper.method = "circle",
show.legend = FALSE
)
# Reordering the correlation matrix
# --------------------------------
# using hierarchical clustering
ggcorrplot(corr, hc.order = TRUE, outline.color = "white")
# draw rectangles around the clusters
ggcorrplot(corr, hc.order = TRUE, hc.rect = 3, outline.color = "white")
# Types of correlogram layout
# --------------------------------
# Get the lower triangle
ggcorrplot(corr,
hc.order = TRUE, type = "lower",
outline.color = "white"
)
# Get the upeper triangle
ggcorrplot(corr,
hc.order = TRUE, type = "upper",
outline.color = "white"
)
# Change colors and theme
# --------------------------------
# Argument colors
ggcorrplot(corr,
hc.order = TRUE, type = "lower",
outline.color = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726")
)
# Add correlation coefficients
# --------------------------------
# argument lab = TRUE
ggcorrplot(corr,
hc.order = TRUE, type = "lower",
lab = TRUE,
ggtheme = ggplot2::theme_dark(),
)
# Add correlation significance level
# --------------------------------
# Argument p.mat
# Barring the no significant coefficient
ggcorrplot(corr,
hc.order = TRUE,
type = "lower", p.mat = p.mat
)
# Leave blank on no significant coefficient
ggcorrplot(corr,
p.mat = p.mat, hc.order = TRUE,
type = "lower", insig = "blank"
)
# Changing number of digits for correlation coeffcient
# --------------------------------
ggcorrplot(cor(mtcars),
type = "lower",
insig = "blank",
lab = TRUE,
digits = 3
)