Learn R Programming

ggcorrheatmap

ggcorrheatmap is a convenient package for generating correlation heatmaps made with ggplot2, with support for triangular layouts, clustering and annotation. As the output is a ggplot2 object you can further customise the appearance using familiar ggplot2 functions. Besides correlation heatmaps, there is also support for making general heatmaps.

Installation

You can install ggcorrheatmap from CRAN using:

install.packages("ggcorrheatmap")

Or you can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("leod123/ggcorrheatmap")

Example

Below is an example of how to generate a correlation heatmap with clustered rows and columns and row annotation, using a triangular layout that excludes redundant cells.

library(ggcorrheatmap)

set.seed(123)
# Make a correlation heatmap with a triangular layout, annotations and clustering
row_annot <- data.frame(.names = colnames(mtcars),
                        annot1 = sample(letters[1:3], ncol(mtcars), TRUE),
                        annot2 = rnorm(ncol(mtcars)))
ggcorrhm(mtcars, layout = "bottomright",
         cluster_rows = TRUE, cluster_cols = TRUE,
         show_dend_rows = FALSE, annot_rows_df = row_annot)

Or a mixed layout that displays different things in the different triangles.

# With correlation values and p-values
ggcorrhm(mtcars, layout = c("topright", "bottomleft"),
         cell_labels = c(FALSE, TRUE), p_values = c(FALSE, TRUE))

It is also possible to make a normal heatmap, for a more flexible output.

gghm(mtcars, scale_data = "col", cluster_rows = TRUE, cluster_cols = TRUE)

More examples

Below is a showcase more things you could do with the package (the code can be found at the bottom).

More examples can be found in the articles of the package:

library(ggplot2)   # moving legend for plot 1
library(patchwork) # for combining into one big figure

# Clustered triangular heatmap with gaps
plt1 <- ggcorrhm(mtcars, layout = "bottomright",
                 split_diag = TRUE,
                 cluster_rows = TRUE, cluster_cols = TRUE,
                 split_rows = 2, split_cols = 2,
                 names_diag_params = list(angle = -45, hjust = 1.3)) +
  theme(legend.position = "inside", legend.position.inside = c(0.25, 0.75))

# Heatmap with other colour scale
plt2 <- gghm(volcano, col_scale = "A", border_col = NA, legend_order = NA,
             show_names_rows = FALSE, show_names_cols = FALSE)

# Mixed correlation heatmap with circles and correlation values
plt3 <- ggcorrhm(mtcars, layout = c("bl", "tr"), mode = c("21", "none"),
                 legend_order = NA, cell_labels = c(FALSE, TRUE))

# Heatmap with categorical values
set.seed(123)
plt4 <- gghm(matrix(letters[sample(1:3, 70, TRUE)], nrow = 7),
             col_scale = "Pastel2", border_col = "#FFE1FF", legend_order = NA,
             border_lwd = 1, show_names_rows = FALSE, show_names_cols = FALSE)

# Split square heatmap with two colour scales
plt5 <- ggcorrhm(mtcars, layout = c("tr", "bl"), mode = c("hm", "hm"),
                 col_scale = c("RdBu_rev", "G"), split_diag = TRUE,
                 border_lwd = 0.3, border_col = 1,
                 legend_order = NA, show_names_diag = FALSE)

# Annotations and gaps
set.seed(123)
dat6 <- sapply(sample(1:5, 30, TRUE), \(x) {
    c(runif(x, runif(1, 0.5, 0.8), runif(1, 0.81, 1)), runif(20 - x, 0, 0.2))[sample(1:20, 20, FALSE)]
})
plt6 <- gghm(dat6, border_col = 0, col_scale = "D",
             show_names_rows = FALSE, show_names_cols = FALSE,
             legend_order = NA, split_rows = 10, split_cols = c(10, 20),
             annot_cols_df = data.frame(a = sample(letters[1:3], 30, TRUE), b = 1:30),
             annot_cols_side = "top", annot_size = 1, dend_height = 1)

wrap_plots(plt1, plt2, plt3, plt4, plt5, plt6, nrow = 2)

Copy Link

Version

Install

install.packages('ggcorrheatmap')

Monthly Downloads

220

Version

0.3.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Leo Dahl

Last Published

October 5th, 2025

Functions in ggcorrheatmap (0.3.0)

extract_scales

Pick out relevant scales and format for mixed layout.
increment1to8

Increment between 1 and 8.
gghm_tidy

gghm() for long format data.
get_annotation_pos

Calculate positions of annotations for heatmap.
get_colour_scale

Get a Brewer or Viridis colour scale.
get_default_annot_scale

Colour scale dispenser.
isSquare

Check if a matrix is square (and require the same row and colnames).
gghm

Make a heatmap with ggplot2.
ggcorrheatmap-package

ggcorrheatmap: Make Flexible 'ggplot2' Correlation Heatmaps
ggcorrhm

Make a correlation heatmap with ggplot2.
ggcorrhm_tidy

ggcorrhm() for long format data.
prepare_cell_labels

Prepare cell labels in ggcorrhm to pass to gghm
prepare_annotation

Prepare annotation parameters and positions
make_facet_vector

Make vector for facetting
layout_hm

Layout heatmap data for plotting
make_heatmap

Make main heatmap part of plot for gghm.
move_dendrogram

Move coordinates of dendrogram to edges of heatmap
orient_dendrogram

Orient a dendrogram.
prepare_dendrogram

Prepare dendrogram by transforming dendrogram segments.
prepare_facets_tidy

Prepare facets for gghm_tidy
make_legend_order

Make legend order depending on what the plot will contain.
prepare_facets

Prepare facetting columns in the plotting data.
scale_dendrogram

Scale height of dendrogram.
remove_triangle

Remove triangle from symmetric matrix and return long format data.
replace_default

Replace default elements in a named list with corresponding elements in a new list.
prepare_scales

Prepare scales for heatmap.
scale_mat

Scale data rows or columns.
prepare_mixed_param

Prepare parameters for mixed layouts.
prepare_scales_annot

Prepare default colour scales for annotation.
shape_mat_wide

Convert long format matrix to wide.
process_dendrogram

Process dendrogram with customisation options
shape_mat_long

Convert a matrix to long format using row names and column names.
test_cor

Calculate correlations and p-values between columns.
remove_duplicate_scales

Remove duplicate scales.
add_annotation

Add annotations to ggplot heatmap
add_mixed_layout

Add a layout column to long format data for mixed layouts.
cor_long

Make a correlation matrix from long format data.
add_diag_names

Add diagonal names to heatmap.
check_dendrogram_pos

Check that dendrograms are positioned correctly
add_split_diag

Draw triangles in the diagonal to split the it between the two triangles of a square matrix.
apply_dendextend

Successively apply dendextend functions to a dendrogram.
add_dendrogram

Add dendrogram to ggplot object.
check_cell_labels

Check cell labels if they are ok.
default_col_scale

Get default colour scale for non-correlation heatmaps.
check_annot_names_deprecated

Check annotation name parameters for deprecated usage.
default_size_scale_corr

Get a default size scale for correlation heatmaps.
default_col_scale_corr

Get a default colour scale for correlation heatmaps.
check_numeric

Check input numeric arguments for class and length.
cluster_data

Cluster data using hierarchical clustering or use provided clustering.
check_layout

Check that layout and mode are correct
check_logical

Check (supposed) logical values.
default_size_scale

Get default size scale for non-correlation heatmaps.
check_annot_df

Check names of annotation