Code
library(ggalign)
#> Loading required package: ggplot2
Special thanks to the patchwork
project—many core codes of the plot composer process were adapted from patchwork
. We have added new features to better implement ggalign
’s layout functions (stack_layout()
and quad_layout()
), including:
free_align()
free_border()
free_guide()
free_lab()
free_space()
free_vp()
These features have not been pushed to patchwork
because they required significant modification of core code. We attempted to merge them, but the author of patchwork
decided to implement some of these features independently. The latest version of patchwork now includes free_align()
, free_lab()
, and free_space()
functionality under a single function: patchwork::free()
. For more details, see: https://www.tidyverse.org/blog/2024/09/patchwork-1-3-0/.
The plot composer function in ggalign
is align_plots()
, which behaves similarly to cowplot::align_plots()
and patchwork::wrap_plots()
. However, you can directly use align_plots()
with quad_layout()
/ggheatmap()
and stack_layout()
, ensuring that they align correctly by plot panel. Additionally, align_plots()
can align pheatmap
and ComplexHeatmap
objects, although they won’t align by panel area with ggplot2.
library(ggalign)
#> Loading required package: ggplot2
We’ll start with a few well-known example plots:
<- ggplot(mtcars) +
p1 geom_point(aes(mpg, disp))
<- ggplot(mtcars) +
p2 geom_boxplot(aes(gear, disp, group = gear))
<- ggplot(mtcars) +
p3 geom_bar(aes(gear)) +
facet_wrap(~cyl)
<- ggplot(mtcars) +
p4 geom_bar(aes(carb))
<- ggplot(mtcars) +
p5 geom_violin(aes(cyl, mpg, group = cyl))
Either add the plots as single arguments
align_plots(p1, p2, p3, p4, p5)
Or use bang-bang-bang to add a list of plots
align_plots(!!!list(p1, p2, p3), p4, p5)
You can use NULL
to indicate the empty area.
align_plots(p1, NULL, p2, NULL, p3, NULL)
Like patchwork
, if no specific layout is provided, align_plots()
will attempt to create a grid that is as square as possible, with each column and row taking up equal space:
align_plots(p1, p2, p3, p4, ncol = 3)
To adjust the widths of columns, use:
align_plots(p1, p2, p3, p4, widths = c(2, 1))
By default, align_plots()
won’t collect any guide legends. You can use the guides
argument to control which side of the guide legends should be collected. They will be collected to their original side. Here, we use patch_titles()
to indicate the guide legend position (instead of using ggtitle()
). patch_titles()
can add titles on four sides, and the title will be placed between the plot panel and the guide legend.
<- ggplot(mtcars) +
p_right geom_point(aes(hp, wt, colour = mpg)) +
patch_titles("right") +
labs(color = "right")
<- p_right +
p_top patch_titles("top") +
scale_color_continuous(
name = "top",
guide = guide_colorbar(position = "top")
)<- p_right +
p_left patch_titles("left") +
scale_color_continuous(
name = "left",
guide = guide_colorbar(position = "left")
)<- p_right +
p_bottom patch_titles("bottom") +
scale_color_continuous(
name = "bottom",
guide = guide_colorbar(position = "bottom")
)align_plots(p_right, p_bottom, p_top, p_left, guides = "tlbr")
If align_plots()
is nested in another align_plots()
, the nested align_plots()
will inherit the guides
argument from the upper-level align_plots()
. And the top-level align_plots()
won’t collect guide legends from plots within the nested align_plots()
unless the nested align_plots()
collects them first.
The free_guide()
function allows you to override the guides
argument for a single plot.
align_plots(
free_guide(p_right, NULL),
free_guide(p_bottom, NULL),
free_guide(p_top, NULL),
free_guide(p_left, NULL),
guides = "tlbr"
)
You can also specify which guide positions to be collected for individual plots.
align_plots(
free_guide(p_right, "r"),
free_guide(p_bottom, "b"),
free_guide(p_top, "t"),
free_guide(p_left, "l")
)