Learn R Programming

visR (version 0.4.1)

align_plots: 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)

Value

List of ggplot with equal width.

Arguments

pltlist

A list of plots

References

https://stackoverflow.com/questions/26159495

Examples

Run this code
# \donttest{

## 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
)
# }

Run the code above in your browser using DataLab