VAlignPlots | R Documentation |
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.
VAlignPlots(
...,
globalTitle = "",
keepTitles = FALSE,
keepXAxisLegends = FALSE,
nb.columns = 1
)
... |
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. |
from https://stackoverflow.com/questions/41569817/align-multiple-plots-in-ggplot2-when-some-have-legends-and-others-dont
The gtable containing the aligned plots.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.