R 如何引用稍后使用bookdown显示的绘图?

R 如何引用稍后使用bookdown显示的绘图?,r,r-markdown,bookdown,R,R Markdown,Bookdown,R Markdown Cookbook的第页显示了如何创建绘图以及如何在文档中稍后显示绘图: --- 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

R Markdown Cookbook的第页显示了如何创建绘图以及如何在文档中稍后显示绘图:

---
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')`)
我的问题是我不能在课文中引用这个情节。请参阅以下代码和输出:

---
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')`)

请注意,如果我立即显示绘图,则参考效果良好:

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


如何在以后显示绘图,但仍然能够引用它?我知道我可以简单地将区块移动到我希望绘图显示的位置,但这在我的情况下并不理想。

在后面的区块中使用
ref.label
,并在图参考中引用后面的区块名称

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

```

\@ref(fig:cars plot)
替换为
[1](#fig:cars plot)
,即
[链接文本](#ID)
?更多信息。