align_plots: Align multiple ggplot graphs, taking into account the legend

View source: R/utils_visr.R

align_plotsR Documentation

Align multiple ggplot graphs, taking into account the legend

Description

This function aligns multiple ggplot graphs by making them the same width by taking into account the legend width.

Usage

align_plots(pltlist)

Arguments

pltlist

A list of plots

Value

List of ggplot with equal width.

References

https://stackoverflow.com/questions/26159495

Examples



## create 2 graphs
p1 <- ggplot2::ggplot(adtte, ggplot2::aes(x = as.numeric(AGE), fill = "Age")) +
  ggplot2::geom_histogram(bins = 15)

p2 <- ggplot2::ggplot(adtte, ggplot2::aes(x = as.numeric(AGE))) +
  ggplot2::geom_histogram(bins = 15)

## default alignment does not take into account legend size
cowplot::plot_grid(
  plotlist = list(p1, p2),
  align = "none",
  nrow = 2
)

## align_plots() takes into account legend width
cowplot::plot_grid(
  plotlist = visR::align_plots(pltlist = list(p1, p2)),
  align = "none",
  nrow = 2
)



visR documentation built on May 29, 2024, 11:50 a.m.