VAlignPlots: Vertically align a list of plots.

View source: R/VAlignPlots.R

VAlignPlotsR Documentation

Vertically align a list of plots.

Description

This function aligns the given list of plots so that the x axis are aligned. It assumes that the graphs share the same range of x data.

Usage

VAlignPlots(
  ...,
  globalTitle = "",
  keepTitles = FALSE,
  keepXAxisLegends = FALSE,
  nb.columns = 1
)

Arguments

...

The list of plots to align.

globalTitle

The title to assign to the newly created graph.

keepTitles

TRUE if you want to keep the titles of each individual plot.

keepXAxisLegends

TRUE if you want to keep the x axis labels of each individual plot. Otherwise, they are all removed except the one of the graph at the bottom.

nb.columns

The number of columns of the generated graph.

Details

from https://stackoverflow.com/questions/41569817/align-multiple-plots-in-ggplot2-when-some-have-legends-and-others-dont

Value

The gtable containing the aligned plots.

Examples

x = seq(0, 10, length.out = 200)
y1 = sin(x)
y2 = cos(x)
y3 = sin(x) * cos(x)
df1 <- data.frame(x, y1, y2)
df1 <- reshape2::melt(df1, id.vars = "x")
g1 <- ggplot2::ggplot(df1, ggplot2::aes(x, value, color = variable)) + ggplot2::geom_line()
df2 <- data.frame(x, y3)
g2 <- ggplot2::ggplot(df2, ggplot2::aes(x, y3)) + ggplot2::geom_line()
g <- VAlignPlots(g1, g2, globalTitle = "Alignment test")
grid::grid.newpage()
grid::grid.draw(g)

Craig44/stockassessmenthelper documentation built on April 14, 2023, 10:57 a.m.