0

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:

enter image description here

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:

enter image description here

The reference is not found.

What is the problem and how can I fix it?

mans
  • 17,104
  • 45
  • 172
  • 321
  • 1
    Can you try `Figure \@ref(fig:`plot-1-3`)` i.e. `plot-1-3` inside backquotes. as there are `-` – akrun Jun 20 '21 at 19:12
  • 1
    I think akrun want to write \`plot-1-3\`. – polkas Jun 20 '21 at 19:13
  • @akrun I can change - to _ or other chars if that help. Is the problem using this char? – mans Jun 20 '21 at 19:16
  • The `_` is a valid one. So, can you check if that works – akrun Jun 20 '21 at 19:19
  • change the label to plot_1_3 and when DF is created, I am getting this result: Figure @ref(fig:plot_1_3) in generated PDF (text is copied to output without any reference and if I use backquote I am getting Figure @ref(fig: ‘plot-1-3’ in pdf – mans Jun 20 '21 at 19:23
  • when you use `plot_1_3`, you have to change the chunk name as well – akrun Jun 20 '21 at 19:27
  • to be confident that I am not using any char which is not supported, I changed it to plot13 but I am getting this error when knitting: LaTeX Warning: Reference `fig:plot13' on page 6 undefined on input line 333. – mans Jun 20 '21 at 19:28
  • Have you changed the part in ````{r plot-1-3, fig.cap="plot-1-3 cap."}` – akrun Jun 20 '21 at 19:30
  • What I understand from your comment is that when you backquote, it is working, but when you changed it to underscore there is a mismatch i.e. may be the chunk name is not changed – akrun Jun 20 '21 at 19:32
  • check if there is some cache involved in it – akrun Jun 20 '21 at 19:34
  • re started R studio and cleared all output. cleared knit cash, but still have the same problem :( – mans Jun 20 '21 at 19:40
  • @mans I am not following you. Can you confirm whether the `plot-1-3` works with backquote or not? – akrun Jun 20 '21 at 19:49
  • @akrun No It did not work – mans Jun 20 '21 at 19:52
  • I have some other figs that they work and some of them doesn't work. some figs that are early in the document is working well, but some figs later in the document doesn't work – mans Jun 20 '21 at 19:54
  • Then, it is not clear. May be this is related to some other aspect in your code if some of the chunks are working well. Can you try changing the name of one of the fig that works well with `-` and see if that still work. Also, please check if there are duplicate names – akrun Jun 20 '21 at 19:56
  • The working ones have - in their name. – mans Jun 20 '21 at 20:05
  • Then, the issue may be in some other part of the code not showedin the post – akrun Jun 20 '21 at 20:19
  • @akrun Based on your comment that the problem maybe exists in some other places, I started from scratched and I am using a very simple notebook so I can find the error. As the code is very simple, I am not sure why I am not getting the right result. Can you please see if you can re produce my error? – mans Jun 24 '21 at 20:56
  • @mans It is a bit strange because I tried on a different example and it is giving the output correctly. Maybe some indentations etc. – akrun Jun 24 '21 at 22:11
  • From my testing, the `_` is causing the issue. If i change it to `-`, it is working i.e. plot-1-1 – akrun Jun 24 '21 at 22:16
  • According to [here](https://bookdown.org/yihui/bookdown/figures.html) `If you want to cross-reference figures or tables generated from a code chunk, please make sure the chunk label only contains alphanumeric characters (a-z, A-Z, 0-9), slashes (/), or dashes (-). ` – akrun Jun 24 '21 at 22:21
  • 1
    @akrun Thanks for your help. I changed the chunk label to be alpha+number (plot1) but still, the reference can not be found. How can I check that my installation is correct? I think there is a problem with my installation of bookdown installation and in general RStudio installation is correct? – mans Jun 24 '21 at 22:42
  • I used `packageVersion("bookdown") [1] ‘0.22’` on `R 4.1.0` – akrun Jun 24 '21 at 22:43
  • that reference part is a bit confusing. I tried couple of time and copy pasted the one that works from the bookdown and then it worked – akrun Jun 24 '21 at 22:44
  • @akrun what was working for you? – mans Jun 24 '21 at 22:49
  • You can test the example [here](https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html). It is working for me – akrun Jun 24 '21 at 22:52

1 Answers1

0

You have to add the chunk option of fig.cap = '...', as suggested here. A caption is necessary when you want to have a figure number in any output format.

---
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, fig.cap = 'Here is a plot about speed'}
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.
Carlos Luis Rivera
  • 3,108
  • 18
  • 45