Is it better to save a plot in a png and include it in R markdown or to generate it directly ?

After doing all of my analyses in R scripts, I'm now looking to write an R Markdown report. I have previously saved my images in png but I'm wondering if including the code (which the arrangement would be painful) in order to ouput graph directly in R markdown is a better way of doing it ?

Yes, definitely. That is the intended use of rmarkdown.


I save the plots as png and then insert them into the Rmd document for two reasons.

Firstly, the processes I run tend to be too large to be computed in one go, so I run separate processes, one of which saves the plots. These can then be simply inserted into the Rmd document using knitr::include_graphics().

Secondly, I found it difficult to match all the dimensions and dpi settings I needed for the plots using Markdown directly, which is most likely my fault.

However, if you don't have these requirements then using RMarkdown directly is the more straightforward method.

1 Like

Just a side note about this, I think this issue can be overcome by using rmarkdown's built in caching capabilities, that way you don't need to re run computationally expensive code chunks unless is needed.

1 Like

Hello, thanks for your response.

I assume then that yes, I would need caching as mentioned by another user, but also, that arranging plots directly in R Markdown using for example ggarrange from ggpubr package would maybe fit the document better than separately and pasting the pictures together ?