Placing table next to plot in R Markdown (to pdf / latex)

霸气de小男生 提交于 2021-02-20 03:55:07

问题


Is it possible to place a table generated with the xtable (or alternatively the pander) package and a generated plot side-by-side in R markdown knitting to pdf while the rest of the document is not in columns? The following simple example hopefully illustrates the idea:

\begin{multicols}{2}
```{r}
plot(cars)
```

```{r, results='asis'}
library('xtable')
print(xtable(head(cars,5)), type = "latex")
```
\end{multicols}

However, this does not produce the plot. I know that solutions exist using knitr (e.g. here) and for R markdown knitting to HTML (e.g. here) but I don't get them to work for R markdown to pdf.


回答1:


the gridExtra package works for this without having to go into LaTeX hell. Use the grid.arrange function for side by side charts and what-not.

Works on html and PDF outputs.




回答2:


Thank you @nycrefugee this already opens some more opportunities. However, there seem to be some problems with xtable outputs. If I use the following code:

library('gridExtra')
library('grid')
library('xtable')
library('lattice')

p = xyplot(1~1)
t = textGrob( print(xtable(head(cars,5)),
                    type = "latex", label = "test")
            )

grid.arrange(p, t, ncol=2)

it produces the following output when compiled to pdf, i.e. the table is shown above the two grobs:

PDF output



来源:https://stackoverflow.com/questions/50070167/placing-table-next-to-plot-in-r-markdown-to-pdf-latex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!