How to reference a plot that is displayed later with bookdown? - r

This section of the R Markdown Cookbook shows how to create a plot and to display it later in the document:
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, dev='png', fig.show='hide'}
plot(cars)
```
After another paragraph, we introduce the plot:
![A nice plot.](`r knitr::fig_chunk('cars-plot', 'png')`)
The problem I have is that I can't reference this plot in the text. See the following code and output:
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, dev='png', fig.show='hide'}
plot(cars)
```
Here's a reference to this plot: figure \#ref(fig:cars-plot).
After another paragraph, we introduce the plot:
![A nice plot.](`r knitr::fig_chunk('cars-plot', 'png')`)
Note that if I display the plot immediately, the reference works well:
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, fig.cap="A nice plot."}
plot(cars)
```
Here's a reference to this plot: figure \#ref(fig:cars-plot).
How can display the plot later but still be able to reference it? I know that I could simply move the chunk where I want the plot to appear but this is not ideal in my case.

Use ref.label in a later chunk and refer to the later chunk name in the figure reference.
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, dev='png', fig.show='hide'}
plot(cars)
```
Here's a reference to this plot: figure \#ref(fig:cars-plot-show).
After another paragraph, we introduce the plot:
```{r cars-plot-show, ref.label="cars-plot", fig.cap="A Nice Plot"}
```

Related

RMarkdown suppress automatic figure numbering to html

Is there a way to suppress automatic figure numbering made by rmarkdown? The first plot rendered by R is the second figure, it added a Figure 1: prefix which is not what I need. I see a solution for PDF output but not html output.
Best,
Shixiang
It's difficult to know what you are looking for without a minimal reproducible example. But maybe you are looking for bookdown::html_document2 output.
Try the following:
---
title: "Example Document"
author: "Your name here"
output:
bookdown::html_document2
---
```{r cars, fig.cap = "plot"} # Figure with caption
plot(cars)
```
```{r cars3, fig.cap = ""} # Figure without caption
plot(cars)
```
This is the output of the above:

Multiple plots in a single row

From R for Data Science:
To put multiple plots in a single row I set the out.width to 50% for
two plots, 33% for 3 plots, or 25% to 4 plots, and set fig.align =
"default". Depending on what I’m trying to illustrate ( e.g. show data
or show plot variations), I’ll also tweak fig.width, as discussed
below.
How do I put multiple plots on a single row, using the method described above? I could use a package such as patchwork, but the purpose of this post is to understand what's being described above. The R Markdown below doesn't generate what I'd expect, two 'pressure' plots on the same row.
---
title: "Untitled"
author: "April 2018"
date: "4/11/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Text
```{r pressure1, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(pressure)
plot(pressure)
```
```{r pressure2, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(pressure)
```
I’ve got no idea why your code (or rather, the code from R for Data Science) is not working. But plotting different data makes it work for me:
```{r pressure1, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(cars)
plot(pressure)
```
Alternatively, it seems to be enough to just specify different parameters, e.g.:
plot(pressure)
plot(pressure, main = '')
The fact that inconsequential changes fix the output indicates to me that this is a bug in RMarkdown.
That said, the easiest, most controlled way is to put par(mfrow = c(1, 2)) into the hunk directly before the plotting commands (or a ggplot2 solution such as faceting or {patchwork}).
Tweaking the alignment via the RMarkdown options can be tricky, due to spacing inserted by the conversion to HTML.
You need to specify fig.show = "hold" along with out.width, as you can see here: https://bookdown.org/yihui/rmarkdown-cookbook/figures-side.html

Graphics on next slide with ioslides using Rstudio's Rmd

I want to have my graph output display on the next slide while the code chunk stays on the first. I am using the default ioslides in Rstudio. I would think that it would be some attribute of the code chunk but I can't figure out what it is.
---
output: ioslides_presentation
---
## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```
Any one have any idea on how to do this in Rstudio?
I want to use this for educational purposes. First showing the code and than revealing the graph. Now I am stuck with doing double code chunks with echo=FALSE and TRUE and eval=FALSE and TRUE.
It's easier to run the code twice, once not evaluating the code, the second time not showing the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot 1
```{r, eval = FALSE}
plot(1:10)
```
## Plot 2
```{r, echo = FALSE}
plot(1:10)
```
It also avoids the error when smaller is true.
To move a plot to the next slide, you need to add a horizontal rule ---- before it (see documentation). You can modify the default plot hook to do it:
```{r setup, include=FALSE}
library(knitr)
local({
hook_plot = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
paste0('\n\n----\n\n', hook_plot(x, options))
})
})
```
If you want all your plots to appear on the next slide Yihui's answer is the way to go. But if you want some plots to appear on the same slide you may be better off doing it manually, similar to what you are already doing (and what Dario suggested). Except that I would strongly recommend the use of chunk references. That way you avoid the need to duplicate the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot code
```{r cars_plot, echo = TRUE, eval = FALSE}
plot(cars)
```
## Plot display
```{r cars_plot, echo = FALSE, eval = TRUE}
```
```
I agree this would be desirable, but I'm finding ioslides to be buggy. Ioslides project started eith google, but was let go a while ago.
You may have better luck with beamer/home slides. But does require a LaTeX distribution to be installed.

How can I incrementally build a plot in an ioslide presentation using Rmarkdown?

I'm trying to build a plot incrementally on one slide using Rmarkdown to create an ioslides (HTML) presentation. I've tried using {.build} but to no avail, and it seems there is no documentation on this in the Rmarkdown community. Does anyone know how to write this code in R?
Here is a MWE where I try to build a plot in three steps, from a blank plot to a scatterplot to a lineplot, all in one slide where the plots unfold by clicking forward one at a time:
---
title: "Untitled"
author: "User"
date: "07/03/2014"
output: ioslides_presentation
---
## Slide Title
```{r, echo=FALSE}
x <- rnorm(10)
```
```{r, echo=FALSE, fig.align='center'}
plot(1:10,x,type='n')
```
```{r, echo=FALSE, fig.align='center'}
plot(1:10,x,type='p')
```
```{r, echo=FALSE, fig.align='center'}
plot(1:10,x,type='b')
```
Any thoughts on how to get this?
After the slide title
<div class="incremental">
And then at the end
</div>

Is there a way to hide figure captions when using knitr and pandoc to create docx files?

I am using knitr and pandoc to write reports into word (we need to be able to circulate for comments using track changes etc).
It is working very well so far, but I have found that the plots are all coming out with captions at the bottom, and I don't want captions. While I could just delete them in the word doc, if I can stop them showing in the code it would be better.
So for the following code in markdown:
Test test test
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6}
plot(cars)
```
I then run the following code in R:
library("knitr")
# Stackoverflow table test 1.html
knit2html("captiontest.rmd")
FILE <- "captiontest"
system(paste0("pandoc -o ", FILE, ".docx ", FILE, ".md"))
And the graph, in the word document, has the caption "plot of chunk unnamed-chunk-2"
I know I can change this caption, e.g. {r fig.width=7, fig.height=6, fig.cap='hello'}, but I thought that fig.cap=NULL would make it hidden. Instead it seems to make the whole plot disappear.
Are plots required to have a caption - do I just have to go through each word doc and delete them manually? Or is there a way to hide them?
Kind of a dirty trick, but:
You can set fig.cap="" on the chunk in question:
Test test test
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6, fig.cap=""}
plot(cars)
```
Or, you can set fig.cap="" for all chunks at once in an initializing chunk at the beginning of your Rmd document:
Test test test
```{r options-chunk}
opts_chunk$set(fig.cap="")
```
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6}
plot(cars)
```

Resources