R Markdown HTML Number Figures
The other answers provided are relatively out of date, and this has since been made very easy using the bookdown package. This package provides a number of improvements which includes the built-in numbering of figures across Word, HTML and PDF.
To be able to use bookdown, you need to first install the package install.packages("bookdown")
and then use one of the output formats. For HTML, this is html_document2
. Taking your example:
---title: "My Title"author: "Me"date: "1/1/2016"output: bookdown::html_document2---```{r cars, fig.cap = "An amazing plot"}plot(cars)``````{r cars2, fig.cap = "Another amazing plot"}plot(cars)```
These Figures will be numbered Figure 1
and Figure 2
. Providing the code chunk is named and has a caption, we can cross reference the output using the the syntax \@ref(fig:foo)
where foo
is the name of the chunk i.e. \@ref(fig-cars)
. You can learn more about this behaviour here
Further Reading
- R Markdown: The definitive Guide: Chapter 11 provides a great overview of bookdown
- Authoring books with bookdown provides a comprehensive guide on bookdown, and recommended for more advanced details.
So unless someone has a better solution, this is the solution that I came up with, there are some flaws with this approach (for example, if the figure/table number is dependent on the section number etc...), but for the basic html document, it works.
Somewhere at the top of you document, run this:
```{r echo=FALSE}#Determine the output format of the documentoutputFormat = opts_knit$get("rmarkdown.pandoc.to")#Figure and Table Caption Numbering, for HTML do it manuallycapTabNo = 1; capFigNo = 1;#Function to add the Table NumbercapTab = function(x){ if(outputFormat == 'html'){ x = paste0("Table ",capTabNo,". ",x) capTabNo <<- capTabNo + 1 }; x}#Function to add the Figure NumbercapFig = function(x){ if(outputFormat == 'html'){ x = paste0("Figure ",capFigNo,". ",x) capFigNo <<- capFigNo + 1 }; x}```
Then during the course of your document, if say you want to plot a figure:
```{r figA,fig.cap=capFig("My Figure Caption")base = ggplot(data=data.frame(x=0,y=0),aes(x,y)) + geom_point()base```
Substitute the capFig
to capTab
in the above, if you want a table caption.
We can make use of pandoc-crossref, a filter that allows a cross-referencing of figures, tables, sections, and equations and works for all output format. The easiest way is to cat
the figure label (in the form of {#fig:figure_label}
) after each plot, although this requires echo=FALSE
and results='asis'
. Then we can reference a figure as we would a citation : [@fig:figure_label]
produces fig. figure_number
by default.
Here is a MWE:
---output: html_document: toc: true number_sections: true fig_caption: true pandoc_args: ["-F","pandoc-crossref"]---```{r}knitr::opts_chunk$set(echo=FALSE,results='asis')``````{r plot1,fig.cap="This is plot one"}x <- 1:10y <- rnorm(10)plot(x,y)cat("{#fig:plot1}")```As we can see in [@fig:plot1]... whereas [@fig:plot2] shows...```{r plot2, fig.cap="This is plot two"}plot(y,x)cat("{#fig:plot2}")```
which produces (removing the graphics
PLOT1
Figure 1: This is plot one
As we can see in fig. 1… whereas fig. 2 shows…
PLOT2
Figure 2: This is plot two
See the pandoc-crossref readme for more options and customizations.
To install pandoc-crossref, assuming you have a haskell installation:
cabal updatecabal install pandoc-crossref