14.4 Generate a plot and display it elsewhere

Normally plots generated in a code chunk are displayed beneath the code chunk, but you can choose to show them elsewhere and (optionally) hide them in the code chunk. Below is an example:

We generate a plot in this code chunk but do not show it:

```{r cars-plot, dev='png', fig.show='hide'}

After another paragraph, we introduce the plot:

![A nice plot.](`r knitr::fig_chunk('cars-plot', 'png')`)

In the code chunk, we used the chunk option fig.show='hide' to hide the plot temporarily. Then in another paragraph, we called the function knitr::fig_chunk() to retrieve the path of the plot file, which is usually like test_files/figure-html/cars-plot-1.png. You need to pass the chunk label and the graphical device name to fig_chunk() for it to calculate the plot file path.

You may see https://stackoverflow.com/a/46305297/559676 for an application of fig_chunk() to blogdown websites. This function works for any R Markdown output formats. It can be particularly helpful for presenting plots on slides, because the screen space is often limited on slide pages. You may present code on a slide, and reveal the plot on a different slide.