Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Markdown HTML Number Figures

Does anyone know how to number the figures in the captions, for HTML format R Markdown script?

For PDF documents, the caption will say something like:

Figure X: Some Caption Text

However, the equivalent caption for the HTML version will simply say:

Some Caption Text

This makes cross-referencing figures by number completely useless.

Here is a minimal example:

---
title: "My Title"
author: "Me"
output:
  pdf_document: default
  html_document: default
---

```{r cars, fig.cap = "An amazing plot"}
plot(cars)
```


```{r cars2, fig.cap = "Another amazing plot"}
plot(cars)
```

I have tried setting toc, fig_caption and number_sections within each of the output formats, but this does not seem to change the result.

like image 824
Nicholas Hamilton Avatar asked May 09 '16 13:05

Nicholas Hamilton


3 Answers

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.
like image 71
Michael Harper Avatar answered Nov 01 '22 17:11

Michael Harper


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 document
outputFormat   = opts_knit$get("rmarkdown.pandoc.to")

#Figure and Table Caption Numbering, for HTML do it manually
capTabNo = 1; capFigNo = 1;

#Function to add the Table Number
capTab = function(x){
  if(outputFormat == 'html'){
    x = paste0("Table ",capTabNo,". ",x)
    capTabNo <<- capTabNo + 1
  }; x
}

#Function to add the Figure Number
capFig = 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.

like image 23
Nicholas Hamilton Avatar answered Nov 01 '22 19:11

Nicholas Hamilton


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:10
y <- 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 update
cabal install pandoc-crossref
like image 5
scoa Avatar answered Nov 01 '22 17:11

scoa