我正在使用R Markdown输出到pdf,并且试图将表格和绘图水平并排对齐。我可以得到fig.align = "right"
来将图对齐到页面的右侧,但是它被绘制在表格的下方(格式为kable
),而不是并排放置。有提示吗?
答案 0 :(得分:3)
这是使用TeX软件包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}
答案 1 :(得分:2)
我更喜欢Martin的方法,但是如果您想要一个不太依赖LaTeX的解决方案,则可以将表格转换为网格图形并plot it as a subfigure:
---
header-includes:
- \usepackage{subfig}
output: pdf_document
---
```{r, fig.cap='two plots', fig.subcap= c('A figure', 'A table'), out.width = '.49\\linewidth', echo = F, fig.align='center'}
library(gridExtra)
library(grid)
plot(mpg ~ hp, data = mtcars)
grid.newpage()
grid.table(head(mtcars[,1:6]), theme = ttheme_minimal())
```
答案 2 :(得分:0)
我能够结合使用multicol
软件包和小型页面来做到这一点。另一个选择...
代码如下:
---
title: "Untitled"
header-includes:
- \usepackage{multicol}
- \newcommand{\btwocol}{\begin{multicols}{2}}
- \newcommand{\etwocol}{\end{multicols}}
output:
pdf_document:
keep_tex: true
---
```{r minipage_funs, echo = FALSE}
## adding minipages in Rmarkdown only seems to work for me when returned from function
fig_table_mp_start <- function() {
return("\\begin{minipage}{\\textwidth}")
}
fig_table_mp_end <- function() {
return("\\end{minipage}")
}
```
`r fig_table_mp_start()`
\btwocol
```{r, fig.align = "right", echo = FALSE}
plot(mpg ~ hp, data = mtcars)
```
```{r, fig.align = "right", echo = FALSE}
knitr::kable(head(mtcars[,1:3]), format = "latex")
```
\etwocol
`r fig_table_mp_end()`
我认为您可以尝试使用填充物使它看起来更漂亮。