【问题标题】:Rmarkdown Latex Align a figure and a kbl table (output PDF)Rmarkdown Latex 对齐图和kbl表(输出PDF)
【发布时间】:2021-03-20 20:32:01
【问题描述】:

我有以下 Rmarkdown 试用版:

---
title: "Test kableExtra"
date: "3/20/2021"
output: pdf_document
classoption: landscape
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(kableExtra)

```

# R Markdown

```{r echo=FALSE}
library(knitr)
library(xtable)

dt <- mtcars[1:5, 1:6]

t1 <- kbl(dt, booktabs = T)
t2 <- kbl(dt, booktabs = T)

t3 <- ggplot(dt,aes(x=mpg,y=cyl)) +
  geom_point()

png(file="plot1.png",width=200,height=200)
print(t3)
dev.off() # close the png file

```
Some Text

\begin{table}[!h]
    \begin{minipage}{.5\linewidth}
      \centering
```{r echo=FALSE}
t1
```
\end{minipage}%
    \begin{minipage}{.5\linewidth}
      \centering
    \includegraphics[width=\textwidth]{plot1.png}
    \end{minipage}
\end{table}

output

当你编织它时它工作正常,但我希望图表和表格在顶部对齐。 有人出主意吗?

如果您有更好的主意将 ggplot 放在 kbl 表旁边,我会全力以赴!

谢谢, 迈克尔

【问题讨论】:

    标签: r ggplot2 latex r-markdown


    【解决方案1】:

    以下代码可能会有所帮助:

    ---
    title: "Test kableExtra"
    date: "3/20/2021"
    output: pdf_document
    header-includes:
      - \usepackage{multicol}
      - \newcommand{\btwocol}{\begin{multicols}{2}}
      - \newcommand{\etwocol}{\end{multicols}}
    classoption: landscape
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(tidyverse)
    library(kableExtra)
    ```
    
    # R Markdown
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
    nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore 
    eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
    in culpa qui officia deserunt mollit anim id est laborum.
    
    \btwocol
    
    ```{r echo=FALSE, results="asis", message=FALSE}
    # sample data
    dt <- mtcars[1:5, 1:6]
    
    # create the table
    t1 <- kable(dt, booktabs = TRUE)
    
    # create the plot
    t3 <- ggplot(dt,aes(x=mpg, y=cyl)) +
      geom_point()
    
    # print the table 
    print(t1)
    
    cat("\\columnbreak")
    
    # print the plot
    print(t3)
    
    cat("\\pagebreak")
    ```
    
    \etwocol
    

    注意事项:

    1. 从概念上讲,解决方案在接受的答案here 中提出;但是,我无法在那里重现代码。

    2. 我删除了您的初始代码以留下一个最小的示例。也就是说,我删除了表t2 的代码以及library(knitr)library(xtable) 的调用。

    3. 对初始代码的其他更改包括:

      • 在 Rmd 文件的 YAML 部分中 header-includes 下对 LaTeX 包和命令的调用

      • 添加了一些 lorem ipsum 文本以更清楚地查看对象是如何相互对齐的

      • 在生成表格和绘图的 R 代码块中设置 results="asis"

      • 使用函数kable() 代替kbl()。如果您使用kbl(),则该表相对于情节有点偏离(我不知道为什么)。我建议您同时使用这两个功能

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 1970-01-01
      • 2020-06-05
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多