This section 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).
如何稍后显示绘图但仍然能够引用它?我知道我可以简单地将 block 移动到我想要绘图出现的位置,但这对我来说并不理想。
最佳答案
在后面的 block 中使用ref.label
,并在图形引用中引用后面的 block 名称。
---
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"}
```
关于r - 如何引用稍后通过 bookdown 显示的绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67175717/