如何解决如何引用稍后通过 bookdown 显示的图?
R Markdown Cookbook 的This section 展示了如何创建绘图并稍后在文档中显示它:
---
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,fig.show='hide'}
plot(cars)
```
Here's a reference to this plot: figure \@ref(fig:cars-plot).
After another paragraph,'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"}
```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。