I have this chunk in an R markdown:
```{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)
```
and I am referring to it such as this:
Figure \@ref(fig:plot-1-3)
when I knit it to pdf, I am getting this error:
LaTeX Warning: Reference `fig:plot-1-3' on page 6 undefined on input line 333.
I have some other plots that are referenced properly, but this one. Any reason for this?
I am using the bookdown package.
Edit 1
Based on suggestions, I change the chunk to this:
```{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)
```
and reference it like this:
\@ref(fig:plot13)
but I am still getting this error:
LaTeX Warning: Reference `fig:plot13'
Edit 2
To find the source of the problem ( and also learn what I did wrong), I created a new R notebook using "New R notebook".
I the generated file, I changed it a bit and the new source is as follow:
---
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.
when I knit it, I am getting the result:
What is the problem and how Can I fix it?
Edit 3
based on comments I change the simple sample to this:
---
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.
But I am still getting this error:
The reference is not found.
What is the problem and how can I fix it?