admin管理员组

文章数量:1293902

I'm using Knitr to generate a PDF report where each page is structured using the multicol environment. The layout consists of two columns:

The first column contains a table. The second column contains a ggsurvminer-generated plot.

However, the issue I'm facing is that the plot starts higher than the table, causing misalignment between the two elements. Ideally, I want both to be aligned at the same level on the page.

MRE:

#```{r config, include=FALSE}
newslide <- function(content) {
  code <- deparse(substitute(content))

  cat(sep = "\n",
    knitr::knit_child(
      quiet = TRUE,
      text = c("```{r dpi=600, warning=FALSE, message=FALSE, echo=FALSE, results='asis', escape = TRUE}",
    code, 
    "```")
      )
  )
}
#```

#```{r latex, echo=FALSE, results = 'asis', escape = "TRUE"}
plot_list <- list()
category <- c("A","B","C")

for (cat in category){
  fit <- survfit(Surv(time, status) ~ sex, data = lung)
  p <- ggsurvplot(fit, data = lung)
plot_list[[cat]] <- p
}


for (cat in category){
  plot1 <- plot_list[[cat]]$plot #I've also tried without "$plot"
  table1_latex <- kable(head(lung), format = "latex")
      newslide(
  content = {
        cat("\\begin{multicols}{2}\n")
        cat("\\noindent\n")
        print(table1_latex)
        cat("\\columnbreak\n")
        cat("\\begin{center}\n")
        print(plot1)
        cat("\n")
        cat("\\end{center}\n")
        cat("\\end{multicols}\n")
  }
)
}

As you can see in the screenshot from my original code, the plot is even overlapping the header before it:

Thank you for any suggestions!

I'm using Knitr to generate a PDF report where each page is structured using the multicol environment. The layout consists of two columns:

The first column contains a table. The second column contains a ggsurvminer-generated plot.

However, the issue I'm facing is that the plot starts higher than the table, causing misalignment between the two elements. Ideally, I want both to be aligned at the same level on the page.

MRE:

#```{r config, include=FALSE}
newslide <- function(content) {
  code <- deparse(substitute(content))

  cat(sep = "\n",
    knitr::knit_child(
      quiet = TRUE,
      text = c("```{r dpi=600, warning=FALSE, message=FALSE, echo=FALSE, results='asis', escape = TRUE}",
    code, 
    "```")
      )
  )
}
#```

#```{r latex, echo=FALSE, results = 'asis', escape = "TRUE"}
plot_list <- list()
category <- c("A","B","C")

for (cat in category){
  fit <- survfit(Surv(time, status) ~ sex, data = lung)
  p <- ggsurvplot(fit, data = lung)
plot_list[[cat]] <- p
}


for (cat in category){
  plot1 <- plot_list[[cat]]$plot #I've also tried without "$plot"
  table1_latex <- kable(head(lung), format = "latex")
      newslide(
  content = {
        cat("\\begin{multicols}{2}\n")
        cat("\\noindent\n")
        print(table1_latex)
        cat("\\columnbreak\n")
        cat("\\begin{center}\n")
        print(plot1)
        cat("\n")
        cat("\\end{center}\n")
        cat("\\end{multicols}\n")
  }
)
}

As you can see in the screenshot from my original code, the plot is even overlapping the header before it:

Thank you for any suggestions!

Share Improve this question asked Feb 12 at 14:30 carolinasccarolinasc 791 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I gave up and used minipages like this:

  newslide(
    content = {
            cat("\\begin{minipage}{0.4\\textwidth}\n")
            print(table2)
            cat("\\end{minipage}")
            cat("\\hspace{0.1\\textwidth}")
            cat("\\begin{minipage}{0.4\\textwidth}\n")
            print(plot1)
            cat('\n\n')
            cat("\\end{minipage}")
    }
  )

本文标签: rMisalignment in Multicol enviroment (Knitr)Stack Overflow