r - 如何引用稍后通过 bookdown 显示的绘图?

标签 r r-markdown bookdown

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

enter image description here

请注意,如果我立即显示该图,则引用效果很好:

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

enter image description here

如何稍后显示绘图但仍然能够引用它?我知道我可以简单地将 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/

相关文章:

r - 使用 stargazer 分析包含时间序列的数据帧

Rcpp:将编译后的函数保存为 Robj

r - Rmarkdown YAML 中的引号和内联 R 代码

r - R Markdown flex_dashboard 中的 Fig.width 和 Fig.height

r - blogdown + hugo,标签式代码块不显示主题 tranquilpeak

r - 如何为 bookdown::tufte_book2() 设置双面对称布局

r - 多个图例/指南的订购(什么是自动逻辑以及如何更改它?)

regex - r 删除带有数字的行

html - 如何增加 bookdown 中侧边栏菜单的宽度

r - Rmarkdown 中的 Stata 语法高亮显示