Printing ggplots out of list does not work in knit

2019-07-10 06:58发布

I would like to put ggplots into a list and then later print each one in the list. That should be done with rmarkdown and knitr in RStudio. MWE (of course, in real, the list gets filled somewhere else than printed):

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

The LaTeX code I get is however

\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}{}!

Of course the result is not pretty. What happended here?

1条回答
闹够了就滚
2楼-- · 2019-07-10 07:17

This is a bug recently introduced in knitr 1.12, and I have fixed it in the development version (>= 1.12.2): https://github.com/yihui/knitr#installation

查看更多
登录 后发表回答