R 降价链接到一个数字

R markdown linking to a figure

我正在创建包含多个图形和表格的报告。我想在随附的文本中提及它们。我尝试了以下方法:

---
title: "Test"
output: 
  pdf_document
---
Figure \ref{test} is a graph

```{r test, fig.cap="This is a graph"}

df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
                 y = rnorm(30))

ggplot(df, aes(x = gp, y = y)) +
   geom_point()
```

This is text to follow the diagram

\pagebreak

This is another page but can still link to Figure \ref{test}

但结果是:

Figure ?? is a graph
...
This is another page but can still link to Figure ??

在 R markdown 中是否有默认的方法可以做到这一点而不必 write functions 我自己

我想我在这里找到了答案- https://github.com/yihui/knitr/issues/323

如果我理解正确的话,使用这段代码似乎可以提供我认为您正在寻找的行为。

---
title: "Test"
output: 
  pdf_document
---
Figure \ref{fig:plot} is a graph

```{r plot-ref, fig.cap = "This is a graph\label{fig:plot}"}

library('ggplot2')

df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
                 y = rnorm(30))

ggplot(df, aes(x = gp, y = y)) +
   geom_point()
```

This is text to follow the diagram

\pagebreak

This is another page but can still link to Figure \ref{fig:plot}