【发布时间】:2016-01-13 14:00:29
【问题描述】:
我想将 ggplots 放入一个列表中,然后打印列表中的每一个。这应该通过 RStudio 中的 rmarkdown 和 knitr 来完成。 MWE(当然,实际上,列表会在其他地方填写而不是打印出来):
---
title: "nice title"
author: "Me"
date: 12\. Januar 2016
output:
pdf_document:
fig_caption: yes
fig_width: 7
keep_tex: yes
number_sections: yes
toc: yes
html_document: default
---
Here is my plot:
```{r, echo=F}
library(ggplot2)
printGGlist <- function(gglist){
for(gg in gglist){
print(gg)
}
}
g1 <- ggplot(diamonds, aes(carat, cut)) + geom_point()
g2 <- ggplot(diamonds, aes(carat, cut)) + geom_density()
gg <- list(g1, g2)
printGGlist(gg)
```
more text
我得到的 LaTeX 代码是
\includegraphics{MyProject_files/figure-latex/unnamed-chunk-7-1.pdf}!
\href{MyProject_files/figure-latex/unnamed-chunk-7-2.pdf}{}!
\href{MyProject_files/figure-latex/unnamed-chunk-7-3.pdf}{}!
当然,结果并不漂亮。这里发生了什么?
【问题讨论】:
-
我不知道为什么它不适合你,但是如果你用
lapply(gg, function(x) x)替换printGGlist(gg)就可以了。 -
另外,指定
fig.align='center'也可以。 -
@toldo:这行得通——但为什么另一件事行不通?
-
我还需要设置
results='hide'btw。
标签: r compilation latex knitr r-markdown