I’m working on an R Markdown project and I’m having trouble getting my plots to show up where I want them in the document.
I need to create a loop that prints 3 tables (using kableExtra) and 3 plots (from ggsurvplot) for each page of a PDF. Here’s the layout I’m trying to achieve for each page:
Table 1 Plot 1
Plot 2 Table 2
Table 3 Plot 3
I’m using the multicol environment to place the tables and plots side by side. All my tables and plots are already saved in lists, so it’s just a matter of getting them to print correctly.
The tables are in the right place (I managed to remove the table enviroment, so that they don't float) but the problem is, the plots aren’t showing up where I want them. Sometimes they all appear at the start or end of the document instead of next to their corresponding tables.
I can get the layout to work if I put the plots and tables in separate chunks, but since I’m working in a loop, I need everything to stay in the same chunk.
I also can’t use floating environments like \[H\] for the plots because they don’t work inside multicol, so I also can't caption them.
How can I print tables and plots side by side in the multicol environment so they appear in the right spots?
I've tried using chunk options like fig.show='hold', fig.show='asis', out.width=50%, and out.extra="" and adding the plots directly inside multicol with cat() and inline LaTeX. None of this has worked. I'm trying to avoid saving the plots as image files.
I expected the plots to be printed where I call them in the code, like this:
#{r table4, echo=FALSE, escape="TRUE", results="asis", fig.align='center'}cat("\begin{multicols}{2}")cat("\\noindent")print(table4_latex)cat("\\columnbreak")cat("\\begin{center}")plot(iris)cat("\\end{center}")cat("\\end{multicols}")
But, when inside a loop, they all appear at the start or end of the document instead of next to their corresponding tables.
I've also tried using grid.arrange() but it doesn't let me turn my KableExtra tables into grub objects.
If anyone has ideas or has run into this issue before, I’d love to hear your suggestions!
Thanks in advance!