【发布时间】:2021-06-20 19:11:11
【问题描述】:
我在 R 降价中有这个块:
```{r plot-1-3, fig.cap="plot-1-3 cap."}
data %>%
mutate(perc = number/sum(number), year = factor(year)) %>%
ggplot(aes(x= year, y= number_of_admissions, fill= sex,label = scales::percent(perc,accuracy=0.1))) +
geom_col(position = 'dodge') +
theme_gray() +
xlab("Year") + ylab("Number") +
theme(legend.title= element_blank(),axis.text.x = element_text(angle = 90))+
scale_fill_discrete(labels = c("Female", "Male")) +
geom_text(aes(y=10000),position = position_dodge(width = .9),angle=90)
```
我指的是这样的:
Figure \@ref(fig:plot-1-3)
当我将它编织成 pdf 时,我收到了这个错误:
LaTeX Warning: Reference `fig:plot-1-3' on page 6 undefined on input line 333.
我还有其他一些被正确引用的图,但是这个。这有什么原因吗?
我正在使用 bookdown 包。
编辑 1
根据建议,我将块更改为:
```{r plot13, fig.cap="plot-1-3 cap."}
data %>%
mutate(perc = number/sum(number), year = factor(year)) %>%
ggplot(aes(x= year, y= number, fill= sex,label = scales::percent(perc,accuracy=0.1))) +
geom_col(position = 'dodge') +
theme_gray() +
xlab("Year") + ylab("Number") +
theme(legend.title= element_blank(),axis.text.x = element_text(angle = 90))+
scale_fill_discrete(labels = c("Female", "Male")) +
geom_text(aes(y=10000),position = position_dodge(width = .9),angle=90)
```
并像这样引用它:
\@ref(fig:plot13)
但我仍然收到此错误:
LaTeX Warning: Reference `fig:plot13'
编辑 2
要找到问题的根源(并了解我做错了什么), 我使用“New R notebook”创建了一个新的 R 笔记本。
我是生成的文件,我改了一下,新的源如下:
---
title: "R Notebook"
output:
bookdown::pdf_document2: default
bookdown::html_document2: default
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r plot_1_1}
plot(cars)
```
Figure \@ref(fig:plot_1_1)
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
当我编织它时,我得到了结果:
问题是什么,我该如何解决?
编辑 3
基于 cmets,我将简单示例更改为:
---
title: "R Notebook"
output:
bookdown::pdf_document2: default
bookdown::html_document2: default
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r plot1}
plot(cars)
```
Figure \@ref(fig:plot1)
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
但我仍然收到此错误:
找不到引用。
问题是什么,我该如何解决?
【问题讨论】:
-
你可以试试
Figure \@ref(fig:plot-1-3),即plot-1-3在反引号内。因为有- -
我觉得akrun想写`plot-1-3`。
-
@akrun 如果有帮助,我可以将 - 更改为 _ 或其他字符。使用这个字符有问题吗?
-
_是有效的。那么,你能检查一下它是否有效 -
@akrun 感谢您的帮助。我将块标签更改为 alpha+number (plot1) 但仍然找不到参考。如何检查我的安装是否正确?我认为我的 bookdown 安装有问题,一般 RStudio 安装是正确的?