Hi,
I have a long document with many similar plots (showing the same variable with different demographic breakdowns), and the labels for the different grouping variables are of different lengths. This leads the plots to look inconsistent as ggplot changes the amount of space allotted for the labels/data area across plots.
I have used cowplot::align_plots to standardize all of my plots.
Unfortunately, I am now having trouble using a loop to display all of the plots. When I try to do so it seems like I am losing the alignment changes. Below is an example rmd doc. If I have to, I can manually list out my plots but I'd like to understand what is breaking when I call plot(ggdraw(x)).
I should also note that these plots will be scattered throughout the report with text in between. I am not trying to create a single plot with multiple panels.
library(ggplot2)
library(cowplot)
Plots are uneven (text / data areas are not consistent)
p1 <- ggplot(iris,
aes(x = Species, y = Petal.Width)) +
geom_boxplot() +
coord_flip()
p1
iris$SpeciesLong <- paste(iris$Species, "extra long text")
p2 <- ggplot(iris,
aes(x = SpeciesLong, y = Petal.Width)) +
geom_boxplot() +
coord_flip()
p2
Plots are consistent
pls <- align_plots(p1, p2, align = "v")
ggdraw(pls[[1]])
ggdraw(pls[[2]])
Loop loses consistency
for(pl in pls) {
plot(ggdraw(pl))
}
# note if run directly in the console, the alignment is not lost, but it is lost in the rmd knitting