# directly copied from `patchwork`
# Sometimes you have a plot that defies good composition alginment, e.g. due
# to long axis labels
p1 <- ggplot(mtcars) +
geom_bar(aes(y = factor(gear), fill = factor(gear))) +
scale_y_discrete(
"",
labels = c(
"3 gears are often enough",
"But, you know, 4 is a nice number",
"I would def go with 5 gears in a modern car"
)
)
# When combined with other plots it ends up looking bad
p2 <- ggplot(mtcars) +
geom_point(aes(mpg, disp))
align_plots(p1, p2, ncol = 1L)
# We can fix this be using `free_align`
align_plots(free_align(p1), p2, ncol = 1L)
# If we still want the panels to be aligned to the right, we can choose to
# free only the left side
align_plots(free_align(p1, axes = "l"), p2, ncol = 1L)
# We could use `free_lab` to fix the layout in a different way
align_plots(p1, free_lab(p2), ncol = 1L)
# `free_border` is similar with `free_lab`, they have a distinction in terms
# of placement on either the top or bottom side of the panel. Specifically,
# the top side contains the `title` and `subtitle`, while the bottom side
# contains the `caption`. free_lab() does not attach these elements in the
# panel area.
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle("Plot 3")
p_axis_top <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle("Plot axis in top") +
scale_x_continuous(position = "top")
align_plots(p_axis_top, free_lab(p3))
align_plots(p_axis_top, free_border(p3))
# Another issue is that long labels can occupy much spaces
align_plots(NULL, p1, p2, p2)
# This can be fixed with `free_space`
align_plots(NULL, free_space(p1, "l"), p2, p2)
Run the code above in your browser using DataLab