We can use bookdown (Xie 2023a) output formats to add figure numbers to their captions. Below is an example:

  1. ---
  2. output: bookdown::html_document2
  3. ---
  4. ```{r cars, fig.cap = "An amazing plot"}
  5. plot(cars)
  6. ```
  7. ```{r mtcars, fig.cap = "Another amazing plot"}
  8. plot(mpg ~ hp, mtcars)
  9. ```

Section 4.7 demonstrates how this works for other elements such as tables and equations, and how to cross-reference the numbered elements within the text. Besides html_document2, there are several other similar functions for other output formats, such as pdf_document2 and word_document2.

You can add this feature to R Markdown output formats outside bookdown, too. The key is to use those formats as the “base formats” of bookdown output formats. For example, to number and cross-reference figures in the rticles::jss_article format, you can use:

  1. output:
  2. bookdown::pdf_book:
  3. base_format: rticles::jss_article

Please read the help pages of the bookdown output format functions to see if they have the base_format argument (e.g., ?bookdown::html_document2).

References

———. 2023a. Bookdown: Authoring Books and Technical Documents with r Markdown. https://github.com/rstudio/bookdown.