sync_width: sync_width

View source: R/functions_misc.R

sync_widthR Documentation

sync_width

Description

synchronize the element widths (axis, plot, etc.) of a list of ggplots.

Usage

sync_width(my_plots)

Arguments

my_plots

A

Details

To work properly the number and order of elements must match between plots. In practical terms, this means either all or non must have vertical facets.

Value

A list of grobs matching input my_plots list of ggplots.

Examples

library(ggplot2)

theme_update(
  plot.title.position = "plot", 
  axis.title.y = element_text(angle = 0, vjust = .5, hjust = 0)
)

p1 = ggplot(mtcars, aes(x = mpg, y = cyl)) +
  geom_point() +
  labs(title = "cylinders vs mpg", y = "cylinder") 

p2 = ggplot(mtcars, aes(x = mpg, y = disp)) +
  geom_point() +
  labs(title = "disp vs mpg") 

p3 = ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_point() +
  labs(title = "horsepower vs mpg") 

plots = list(p1, p2, p3)

#without synchronization, x axis positions do not line up between plots.
cowplot::plot_grid(plotlist = plots, ncol = 1)

#after synchronization, x axis positions line up perfectly between plots.
plots.sync = sync_width(plots)
cowplot::plot_grid(plotlist = plots.sync, ncol = 1)

FrietzeLabUVM/ssvQC documentation built on March 25, 2024, 12:24 a.m.