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 badges1 Answer
Reset to default 0I 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
版权声明:本文标题:r - Misalignment in Multicol enviroment (Knitr) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741592057a2387186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论