【问题标题】:Printing ggplots out of list does not work in knitr with rmarkdown使用 rmarkdown 在 knitr 中打印列表中的 ggplots 不起作用
【发布时间】: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


【解决方案1】:

这是knitr 1.12最近引入的一个bug,我已经在开发版(>= 1.12.2)中修复了它:https://github.com/yihui/knitr#installation

【讨论】:

  • 我猜应该是和devtools::install_github('yihui/knitr')一起安装的吧?如果是这样,请将其添加到您的帖子中,以帮助没有经验的读者;-)。另外:请提及开发版本的稳定性。
  • 该链接现在将带您进入安装说明。通常开发版本非常稳定。我不把不成熟的代码推送到repo,包里面有很多测试。
猜你喜欢
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
相关资源
最近更新 更多