本文介绍了R Markdown - 并排定位表格和绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R Markdown 输出到 pdf,并且我试图获得一个表格和一个水平并排对齐的图.我可以让 fig.align = "right" 将绘图与页面右侧对齐,但它绘制在表格下方(使用 kable 格式化)而不是侧面与它并肩.有什么提示吗?

I am using R Markdown to output to pdf, and I am trying to get a table and a plot aligned side by side horizontally. I can get fig.align = "right" to align the plot to the right of the page, but it is plotted under the table (formatted with kable) and not side by side with it. Any tips?

推荐答案

以下是使用 TeX 包 floatrow 的方法:

Here is a way using the TeX package floatrow:

---
title: "Untitled"
header-includes:
  - \usepackage{floatrow}
output: 
  pdf_document:
    keep_tex: true
---

\newfloatcommand{btabbox}{table}

\begin{figure}[H]
  \begin{floatrow}
    \ffigbox{%
```{r, fig.align = "right", echo = F}
plot(mpg ~ hp, data = mtcars)
```
    }{\caption{A figure}}

    \btabbox{%
```{r, fig.align = "right", echo = F}
knitr::kable(head(mtcars[,1:3]), format = "latex")
```
    }{\caption{A table}}
  \end{floatrow}
\end{figure}

这篇关于R Markdown - 并排定位表格和绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:16