free_align {ggalign} | R Documentation |
align_plots will try to align plot panels, and every elements of the plot, following functions romove these restrictions:
free_align
: if we want to compose plots without alignment of some panel
axes (panel won't be aligned). we can wrap the plot with free_align
.
free_border
: If we want to compose plots without alignment of the panel
borders (but still align the panels themselves), we can wrap the plot with
free_border
.
free_lab
: If we want to compose plots without alignment of the axis
title, we can wrap the plot with free_lab
.
free_space
: Removing the ggplot element sizes when aligning.
free_align(plot, axes = "tlbr")
free_border(plot, borders = "tlbr")
free_lab(plot, labs = "tlbr")
free_space(plot, ...)
plot |
A ggplot or alignpatches object. |
axes |
Which axes shouldn't be aligned? A string containing
one or more of |
borders |
Which border shouldn't be aligned? A string containing
one or more of |
labs |
Which axis labs to be free? A string containing one or more of
|
... |
What sizes of the ggplot2 elements to remove? Allowed values are:
|
free_align
: A modified version of plot
with a free_align
class.
free_border
: A modified version of plot
with a free_border
class.
free_lab
: A modified version of plot
with a free_lab
class.
free_space
: A modified version of plot
with a free_space
class.
# 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 (here, with the default "panel" type)
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 "label" to fix the layout in a different way
align_plots(p1, free_lab(p2), ncol = 1L)
# Another issue is that long labels are not using already available free
# space.
align_plots(NULL, p1, p2, p2)
# This can be fixed with the "space" type
align_plots(NULL, free_space(p1, "l"), p2, p2)