# Sometimes you have a plot that defies good composition alginment, e.g. due
# to long axis labels
library(ggplot2)
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))
p1 / p2
# We can fix this be using free (here, with the default "panel" type)
free(p1) / p2
# If we still want the panels to be aligned to the right, we can choose to
# free only the left side
free(p1, side = "l") / p2
# We can still collect guides like before
free(p1) / p2 + plot_layout(guides = "collect")
# We could use "label" to fix the layout in a different way
p1 / free(p2, "label")
# Another issue is that long labels are not using already available free
# space.
plot_spacer() + p1 + p2 + p2
# This can be fixed with the "space" type
plot_spacer() + free(p1, "space", "l") + p2 + p2
Run the code above in your browser using DataLab