patchwork
patchwork copied to clipboard
Empty space with `coord_fixed`
First off, thanks for a great package; I've been benefitting greatly from patchwork
ing my plots.
Issues in connection with coord_fixed()
have come up a few times before, and I understand that the "best of use of space" can be a subjective request. However in my case of two side-by-side plots with a fixed aspect ratio and common title, I end up with a very large amount of unused space that I can't seem to get rid off.
Here is a reprex:
library(tidyverse)
library(patchwork)
replicate(
2,
ggplot(mtcars, aes(mpg, wt)) + geom_point() + coord_fixed(),
simplify = FALSE) %>%
wrap_plots(ncol = 2) +
plot_layout(heights = 1) +
plot_annotation(title = "Title")
Is there any way to remove the empty space at the bottom and between the top of the plotting area and the title?
A couple of things to note here:
1
If you remove the specification of height. Patchwork will respect the aspect ratio of your figures and adjust the row height accordingly:
library(tidyverse)
library(patchwork)
replicate(
2,
ggplot(mtcars, aes(mpg, wt)) + geom_point() + coord_fixed(),
simplify = FALSE) %>%
wrap_plots(ncol = 2) +
plot_layout() +
plot_annotation(title = "Title")
Created on 2021-08-31 by the reprex package (v2.0.1)
Once you specify a height =1
you are asking from patchwork to use the entire height of the graphics output and thus the title gets placed all the way to the top. This is also shown in the documentation regarding aspect ratio figures: https://patchwork.data-imaginist.com/articles/guides/layout.html#fixed-aspect-plots
2
You might argue that in my example above there is still a lot of white space above and below the figure. However, this is simply because of the dimension of the graphics output. Patchwork cannot influence this.
As you will see this already the case with simply ggplot figures with fixed aspect ratio:
library(tidyverse)
ggplot(mtcars, aes(mpg, wt)) + geom_point() + coord_fixed()
Created on 2021-08-31 by the reprex package (v2.0.1)
If you want to save the image with ggsave()
and do not want to have too much whitespace, you will need to specify the dimensions of your graphics device with height and width arguments that respect your aspect ratio.
Other related posts to this: https://community.rstudio.com/t/ggsave-aspect-ratio-whitespace-use-case-favicon-for-blogdown/6793/6 https://stackoverflow.com/questions/16422847/save-plot-with-a-given-aspect-ratio
I encountered a somewhat similar issue and found a solution that works for me. I wanted 3 plots stacked vertically and I wanted the topmost plot to have coord_fixed. However, using coord_fixed on my top plot, changed the dimensions of the two plots below it. I found using wrap_elements from patchwork on my top plot fixed the issue. I'm posting this here in case it helps others who experience strange behavior with coord_fixed and patchwork.
Reproducible Example: a = data.frame(a = c(1, 2, 3), b = c(1, 4, 9)) p1 = ggplot(a, aes(x = a, y = b)) + geom_point() + coord_fixed() p2 = ggplot(a, aes(x = a, y = b)) + geom_point() p3 = ggplot(a, aes(x = a, y = b)) + geom_point() patchwork::wrap_plots(p1, p2, p3, ncol = 1) # here p1 causes changes in the dimensions of p2 and p3 p1.solution = patchwork::wrap_elements(plot = ggplot(a, aes(x = a, y = b)) + geom_point() + coord_fixed()) patchwork::wrap_plots(p1.solution, p2, p3, ncol = 1)
tldr: use wrap_plots around the plot that you want to have coord_fixed