Hi, I have developed a package called 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")
The latest version has modified a lot, try to use the development version now.
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,
ggalign
works with a simple workflow:
- Initialize the layout using
ggheatmap()
orggstack()
. - Customize the layout with:
align_group()
: Group layout axis into panel with a group variable.align_kmeans()
: Group layout axis into panel by kmeansalign_reorder()
: Reorder layout observations based on statistical weights or allows for manual reordering based on user-defined criteria.align_dendro()
: Reorder or Group layout based on hierarchical clustering
- Adding plots with
ggalign()
orggpanel()
, then add ggplot2 elements like geoms, stats, 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
The main advantage of ggalign
over other extensions like
ggheatmap is its full compatibility
with the ggplot2 grammar. You can seamlessly use any ggplot2 geoms, stats, and
scales to build 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.
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
:
Welcom feature request and issues
If you can get it to play nicely with plotly's
ggplotly()
function, that'd be pretty neato.Sorry, the internal will build a patchwork firstly, but
plotly
doesn't support thepatchwork
. So it's not possible for this to work withggplotly
. Please see https://github.com/plotly/plotly.R/issues/2028 for detailsCan you plot faceted heat maps with this? i.e. If I create n heat maps... I can use do.call/grid.arrange to plot them all on 1 image, but it is awkward, and I still can't figure out how to add a main title, you need R(KungFuSkills) for that.
Yes, it internally use facet to plot the heatmap groups. The dendrogram will be aligned well with any ggplot objects (Just use the
ggalign
function to create aggplot
plot) in the facet layout.I have changed the default theme, and it can support connect multiple heatmap horizontally or vertically.
Because it can now align a
dendrogram
in a standard ggplot2 plot, enhancing its versatility, and it has been renamed into ggalign:Created on 2024-07-17 with [reprex v2.1.0](https://reprex.tidyverse.org) ~
layout_stack
put plots horizontally or vertically. You can also use the aliasggstack
.