I am looking for a method to do two things:
-
standardize ggplot panel size and
-
align panels.
And this should be done across multiple code chunks.
The approaches provided by patchwork::align_patches() and cowplot::align_plots() are both very close. However, they require that the ggplots be built all at once and then dispatched one at a time. Baptiste's egg method in this SO crosses code chunks, but then the alignment is lost (it's also hackish).
This seems like it should be a common use case - do some analysis, plot the result, manipulate the result, plot that, etc., and then have it all consistently sized and aligned along the y axes (assuming that is even possible...).
Reprex
---
output: html_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.align = "center")
```
```{r}
library(ggplot2)
data(mtcars)
```
```{r}
ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2')
```
words, words, words
```{r}
ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
```
With this result: