I am trying to combine two ggplot objects using cowplot::plot_grid()
and vertically align them. This is normally quite simple using align = "v"
.
dat1 <- data.frame(x = rep(1:10, 2), y = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point()
cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v")
However, this approach fails when the ggplots use coord_equal()
because plot_grid()
cannot modify the axes when the aspect ratio is forced. Instead, the default is to keep the heights of each plot the same.
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v")
I can force my objective by playing with and getting the rel_heights
argument just right, but this is not a viable solution as I have many, dynamic plots to build. Here, the y-axes are aligned, and the coordinates of all axes are still equal.
cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v", rel_heights = c(2, 1.07))
I've seen many approaches to similar questions that utilize ggplot2::ggplotGrob()
and grid::grid_draw()
, but nothing quite gets at this issues when coord_equal()
is used. Perhaps the best solution doesn't use cowplot::plot_grid()
at all, or perhaps the solution is somehow dynamically determining and passing the right values to rel_heights
. I think I would prefer the later option so as to be able to easily use the other features that come with cowplot::plot_grid()
. Perhaps some useful inspiration can be found in this related approach.
By default, the range of the axes actually extends a little bit past the limits in the ggplot. The
expand
argument in functionscale_continuous/discrete()
was used to setting the extends. As in thescale_continuous()
documentation:Fist, we can calculate the actual heights of this two plots, this post explains how the
expand
argument work.Then, use patchwork to compose this two plots
Author of
cowplot::plot_grid()
here. It doesn't work when you're trying to align plots with specified aspect ratio, which you generate when usingcoord_equal()
. The solution is to use either the egg library or the patchwork library. Patchwork is still in development but should be released to CRAN soon. In the mean time, you can install from github.Here is a solution using egg. It seems to me that it works just fine.
Two minor issues I see are that (1) the axis ticks for the two y axes are different, and that makes it look like the spacing is different, and (2) the axes are expanded to different limits. You can work around both by manually setting ticks and expansion.