Printing ggplots out of list does not work in knitr with rmarkdown

房东的猫 提交于 2019-12-11 00:29:01

问题


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:


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



来源:https://stackoverflow.com/questions/34768660/printing-ggplots-out-of-list-does-not-work-in-knitr-with-rmarkdown

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