【问题标题】:How to reference a plot that is displayed later with bookdown?如何引用稍后通过 bookdown 显示的图?
【发布时间】:2021-04-20 09:00:21
【问题描述】:

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

如何在稍后显示绘图但仍然可以引用它?我知道我可以简单地将块移动到我希望情节出现的位置,但这对我来说并不理想。

【问题讨论】:

  • [1](#fig:cars-plot) 替换\@ref(fig:cars-plot) 怎么样,即[link text](#ID)?更多信息here.

标签: r r-markdown bookdown


【解决方案1】:

在后面的块中使用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"}

```

【讨论】:

    猜你喜欢
    • 2014-06-10
    • 1970-01-01
    • 2022-01-23
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2011-09-24
    相关资源
    最近更新 更多