r - Printing multiples table and plots side by side with multicol in PDF Knit loop - Stack Overflow

I’m working on an R Markdown project and I’m having trouble getting my plots to show up where I want th

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!

Packages used in YMAL:

  • \usepackage{tocloft}
  • \usepackage[utf8]{inputenc}
  • \usepackage{floatrow}
  • \usepackage{placeins}
  • \usepackage{setspace}
  • \usepackage{geometry}
  • \usepackage{graphicx}
  • \usepackage{titling}
  • \usepackage[dvipsnames]{xcolor}
  • \usepackage{fancyhdr}
  • \usepackage{tikz}
  • \usepackage{hyperref}
  • \usepackage{multicol}
  • \geometry{a4paper, left=2cm, right=2cm, top=1in, bottom=1in}
  • \setlength{\parskip}{0.30em}
  • \setlength{\parindent}{0pt}
  • \usetikzlibrary{positioning}
  • \usepackage{lmodern}
  • \usepackage{booktabs}
  • \usepackage{array}
  • \usepackage[labelformat = empty]{caption}
  • \usepackage{colortbl}
  • \arrayrulecolor{white}
  • \usepackage{tcolorbox}
  • \usepackage{float}
  • \floatplacement{figure}{H}

Minimal Reproducible Example:

---
 title: "Untitled"
 header-includes:
  - \usepackage{multicol}
  - \usepackage[labelformat = empty]{caption} 
 output:
   pdf_document:
     keep_tex: true

---
```{r cars, echo=FALSE, results='asis'}
library(ggplot2)

tables1 <- list()
tables2 <- list()
tables3 <- list()
tables4 <- list()
plots1 <- list()
plots2 <- list()
plots3 <- list()

categories <- c("A","B","C")

for (cat in categories){
tables1[[cat]] <- head(iris)
tables2[[cat]] <- head(cars)
tables3[[cat]] <- head(mtcars)
tables4[[cat]] <- head(ToothGrowth)
plots1[[cat]] <- ggplot(cars)
plots2[[cat]] <- ggplot(mtcars)
plots3[[cat]] <- ggplot(ToothGrowth)
}

for (cat in categories){
  table1 <- tables1[[cat]]
  table2 <- tables2[[cat]]
  table3 <- tables3[[cat]]
  table4 <- tables4[[cat]]
  plot1 <- plots1[[cat]]
  plot2 <- plots2[[cat]]
  plot3 <- plots3[[cat]]
  
  #then i format all the tables with kableExtra, removing the "table" enviroment
  
  cat("\\newpage\n") #a page for each category, containing all the plots and tables in the same order
  cat("\\fontsize{11}{13}\\selectfont\n")
  cat(cat, "\n") #title should be the name of the category
  cat("\\vspace*{-0.1cm}\n\n")
  print(table1) #full-width table
  cat("\\setlength{\\columnsep}{25pt}\n")
  #full-width header
  cat("\\begin{multicols}{2}\n")
  print(table2)
  cat("\\columnbreak\n")
  cat("\\begin{center}\n")
  print(plot1)
  cat("\\end{center}\n")
  cat("\\end{multicols}\n")
  #full-width header
  cat("\\begin{multicols}{2}\n")
  print(table3)
  cat("\\columnbreak\n")
  cat("\\begin{center}\n")
  print(plot2)
  cat("\\end{center}\n")
  cat("\\end{multicols}\n")
  #full-width header
  cat("\\begin{multicols}{2}\n")
  print(table4)
  cat("\\columnbreak\n")
  cat("\\begin{center}\n")
  print(plot3)
  cat("\\end{center}\n")
  cat("\\end{multicols}\n")
}

```

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745296696a4621185.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信