问题
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