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(scale(mtcars), cluster_rows = TRUE, cluster_cols = TRUE)

More examples

There are many more options for customisation, covered in the different articles of the package:

Copy Link

Version

Install

install.packages('ggcorrheatmap')

Version

0.1.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Leo Dahl

Last Published

July 28th, 2025

Functions in ggcorrheatmap (0.1.2)

add_diag_names

Add diagonal names to heatmap.
ggcorrheatmap-package

ggcorrheatmap: Make Flexible 'ggplot2' Correlation Heatmaps
gghm

Make a heatmap with ggplot2.
ggcorrhm

Make a correlation heatmap with ggplot2.
prepare_dendrogram

Prepare dendrogram by transforming dendrogram segments.
prepare_cell_labels

Prepare cell labels in ggcorrhm to pass to gghm
default_scale

Get default scale for non-correlation heatmaps.
get_colour_scale

Get a Brewer or Viridis colour scale.
default_scale_corr

Get a default scale for correlation heatmaps.
move_dendrogram

Move coordinates of dendrogram to edges of heatmap
shape_mat_wide

Convert long format matrix to wide.
cluster_data

Cluster data using hierarchical clustering or use provided clustering.
increment1to8

Increment between 1 and 8.
check_logical

Check (supposed) logical values.
make_legend_order

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

Colour scale dispenser.
remove_triangle

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

Remove duplicate scales.
prepare_mixed_param

Prepare parameters for mixed layouts.
test_cor

Calculate correlations and p-values between columns.
prepare_scales_annot

Prepare default colour scales for annotation.
check_layout

Check that layout and mode are correct
check_numeric

Check input numeric arguments for class and length.
prepare_scales

Prepare scales for heatmap.
shape_mat_long

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

Pick out relevant scales and format for mixed layout.
orient_dendrogram

Orient a dendrogram.
get_annotation_pos

Calculate positions of annotations for heatmap.
replace_default

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

Prepare annotation parameters and positions
process_dendrogram

Process dendrogram with customisation options
scale_dendrogram

Scale height of dendrogram.
layout_hm

Layout heatmap data for plotting
make_heatmap

Make main heatmap part of plot for gghm.
check_dendrogram_pos

Check that dendrograms are positioned correctly
apply_dendextend

Successively apply dendextend functions to a dendrogram.
check_annot_df

Check names of annotation
add_annotation

Add annotations to ggplot heatmap
add_dendrogram

Add dendrogram to ggplot object.