Using a loop to display cowplot::align_plot output in RMD

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

Using results = "asis" is necessary in the for loop chunk. However, there is an additional problem that didn't come up in the reprex, but was present in my actual implementation.

There are alignment issues when using coord_flip() and having an empty value for the x-axis label labs(x = "").

I was able to resolve the issue by using labs(x = " "). I am curious to know what is going on if anyone has any ideas.

Hi @Lief,
Thanks for providing such a good reproducible example :slightly_smiling_face:
When I run your Rmarkdown code and knit to HTML, Word, or PDF I see no alignment problem with the short labels versus the long labels - the 'bodies' of all the plots are in the same vertical position.
Here is a summary of my setup:

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_4.0.2  htmltools_0.5.0 tools_4.0.2     yaml_2.2.1      rmarkdown_2.3  
 [6] knitr_1.29      xfun_0.15       digest_0.6.25   rlang_0.4.6     evaluate_0.14  

I am using {tinytex} (i.e. TinyTex) to render to PDF.
So, I'm not sure why your outputs are different. Maybe the {patchwork} package will give you more consistent results.
HTH

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.