Learn R Programming

⚠️There's a newer version (1.0.1) of this package.Take me there.

ggalign

This package extends ggplot2 by providing advanced tools for aligning and organizing multiple plots, particularly those that automatically reorder observations, such as dendrogram. It offers fine control over layout adjustment and plot annotations, enabling you to create complex, publication-quality visualizations while still using the familiar grammar of ggplot2.

Why use ggalign?

ggalign focuses on aligning observations across multiple plots. It leverages the "number of observations" in the vctrs package or NROW() function to maintain consistency in plot organization.

If you’ve ever struggled with aligning plots with self-contained ordering (like dendrogram), or applying consistent grouping or ordering across multiple plots (e.g., with k-means clustering), ggalign is designed to make this easier. The package integrates seamlessly with ggplot2, providing the flexibility to use its geoms, scales, and other components for complex visualizations.

Installation

You can install ggalign from CRAN using:

install.packages("ggalign")

Alternatively, install the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("Yunuuuu/ggalign")

Getting Started

The usage of ggalign is simple if you’re familiar with ggplot2 syntax, the typical workflow includes:

  • Initialize the layout using ggheatmap() or ggstack().
  • Customize the layout with:
    • align_group(): Group layout axis into panel with a group variable.
    • align_kmeans(): Group layout axis into panel by kmeans.
    • align_order(): Reorder layout observations based on statistical weights or by manually specifying the observation index.
    • align_dendro(): Reorder or Group layout based on hierarchical clustering.
  • Adding plots with ggalign() or ggpanel(), and then layer additional ggplot2 elements such as geoms, stats, or scales.

Basic example

Below, we’ll walk through a basic example of using ggalign to create a heatmap with a dendrogram.

library(ggalign)
set.seed(123)
small_mat <- matrix(rnorm(81), nrow = 9)
rownames(small_mat) <- paste0("row", seq_len(nrow(small_mat)))
colnames(small_mat) <- paste0("column", seq_len(ncol(small_mat)))

# initialize the heatmap layout, we can regard it as a normal ggplot object
ggheatmap(small_mat) +
    # we can directly modify geoms, scales and other ggplot2 components
    scale_fill_viridis_c() +
    # add annotation in the top
    hmanno("top") +
    # in the top annotation, we add a dendrogram, and split observations into 3 groups
    align_dendro(aes(color = branch), k = 3) +
    # in the dendrogram we add a point geom
    geom_point(aes(color = branch, y = y)) +
    # change color mapping for the dendrogram
    scale_color_brewer(palette = "Dark2")

Compare with other ggplot2 heatmap extension

ggalign offers advantages over extensions like ggheatmap by providing full compatibility with ggplot2. With ggalign, you can:

  • Seamlessly integrate ggplot2 geoms, stats, scales et al. into your layouts.
  • Align dendrograms even in facetted plots.
  • Easily create complex layouts, including multiple heatmaps arranged vertically or horizontally.

Compare with ComplexHeatmap

Pros

  • Full integration with the ggplot2 ecosystem.
  • Heatmap annotation axes and legends are automatically generated.
  • Dendrogram can be easily customized and colored.
  • Flexible control over plot size and spacing.
  • Can easily align with other ggplot2 plots by panel area.
  • Can easily extend for other clustering algorithm, or annotation plot.

Cons

Fewer Built-In Annotations: May require additional coding for specific annotations or customization compared to the extensive built-in annotation function in ComplexHeatmap.

More Complex Examples

Here are some more advanced visualizations using ggalign:

Further Documentation

Copy Link

Version

Install

install.packages('ggalign')

Monthly Downloads

774

Version

0.0.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Yun Peng

Last Published

October 12th, 2024

Functions in ggalign (0.0.4)

free_align

Free from alignment
$,Layout-method

Subset a Layout object
ggalign_stat

Get the statistics from the layout
ggalignGrob

Generate a plot grob.
area

Define the plotting areas in align_plots
hclust2

Generate Tree Structures with Hierarchical Clustering
dendrogram_data

Dengrogram x and y coordinates
alignpatch

Prepare plots to be aligned with align_plots
ggalign-package

ggalign: A 'ggplot2' Extension for Consistent Axis Alignment
heatmap_layout

Arrange plots in a Heatmap
layout_title

Annotate the whole layout
layout-add

Add components to Layout
is_ggheatmap

Reports whether x is a heatmap_layout() object
inset

Create a ggplot inset
hmanno

Determine the active context of heatmap layout
order2

Ordering Permutation
layout-operator

Layout operator
patch.patch

Convert Object into a Grob
patch.formula

Convert Object into a Grob
patch.alignpatches

Convert Object into a Grob
patch.patch_ggplot

Convert Object into a Grob
is_ggstack

Reports whether x is a StackLayout object
patch.ggplot

Convert Object into a Grob
patch.grob

Convert Object into a Grob
patch.trellis

Convert Object into a Grob
patch.pheatmap

Convert Object into a Grob
reexports

Objects exported from other packages
patch.patchwork

Convert Object into a Grob
patch.recordedplot

Convert Object into a Grob
show,Layout-method

Print Layout object
wrap

Wrap Arbitrary Graphics for Alignment
read_example

Read Example Data
patch_titles

Add patch titles to plot borders
theme_ggalign

Complete theme for layout plots
layout_annotation

Modify components of the layout
layout_design

Define the grid to compose plots in
patch

Convert Object into a Grob
patch.Heatmap

Convert Object into a Grob
stack_active

Determine the active context of stack layout
stack_layout

Put plots horizontally or vertically
align_order

Order layout observations based on weights
align_group

Group and align layout based on categorical or factor levels.
align_dendro

Reorder or Group layout based on hierarchical clustering
align_gg

Create ggplot object with a customized data
align_kmeans

Split layout by k-means clustering groups.
align_plots

Arrange multiple plots into a grid
align_reorder

Reorders layout observations based on specific statistics.
Layout-class

A Layout object
align_panel

Create ggplot object with layout panel data
align

Create a new Align object