To align plots with intervening text, you can use align_plots
from the cowplot
package. It returns a list where each element is one of the plots, but with all the panels aligned. Here's an rmarkdown
example:
---
output: pdf_document
urlcolor: blue
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, fig.height=1.5, fig.width=4)
library(tidyverse)
library(cowplot)
theme_set(theme_bw())
```
```{r}
key <- c("High", "Medium", "Low")
value <- c(.51, .36, .13)
df1 <- data.frame (key, value)
p1=df1 %>%
ggplot () +
aes (x = key, y = value) +
geom_bar (stat = "identity") +
coord_flip()
key <- c("5+ times a week", "3-4 times a week", "2 or fewer times a week")
value <- c(.87, .04, .09)
df2 <- data.frame (key, value)
p2=df2 %>%
ggplot () +
aes (x = key, y = value) +
geom_bar (stat = "identity") +
coord_flip()
```
Note that the plots below have panels that are aligned vertically on the page, even though there is intervening text between each plot. The code to align the plots is adapted from [this Stack Overflow answer](http://stackoverflow.com/a/45616684/496488).
```{r}
# Generate a list of the aligned plot objects
pl <- align_plots(p1, p2, align="v")
```
```{r}
ggdraw(pl[[1]])
```
`align_plots` returns a list of plots that we can draw seperately, but whose panels will still be vertically aligned on the page.
```{r}
ggdraw(pl[[2]])
```
Here's the output document: