Figures, Plots and Tables

Install Captioner

For the ability to reference figures and table in your text and to automatically number them we need to install an R package called captioner. Run the following command in the Rstudio console, or install using the “Tools/Install Packages…” from the main menu.

>install.packages("captioner")

Here is the ‘How to’ for Captioner.

Create the Captions

Include the following R chunk. (probably at the beginning of your .Rmd file after the title block)

Code
```{r include = FALSE}
    
library(captioner)

fig_nums <- captioner(prefix = "Figure")

fig_nums("a fig", "caption for 'a fig'")
fig_nums("another fig", "caption for 'another fig'")
fig_nums("yet another", "caption for 'yet another'")
fig_nums("a_plot", "a R plot'")

table_nums <- captioner(prefix = "Table")

table_nums("a table", "caption for 'a table'")
table_nums("another table", "caption for 'another table'")
table_nums("yet another table", "caption for 'yet another table'")
```

Reference the Figures

Code
You can reference the figures like this:
`r fig_nums("a fig", display = "cite")`,
`r fig_nums("another fig", display = "cite")` and
`r fig_nums("yet another", display = "cite")`
Result

You can reference the figures like this: Figure 1, Figure 2 and Figure 3

Reference the Tables

Code
You can reference the tables the same way you do the figures above like this:
`r table_nums("a table", display = "cite")`,
`r table_nums("another table", display = "cite")` and
`r table_nums("yet another table", display = "cite")`
Result

You can reference the tables the same way you do the figures above like this: Table 1, Table 2 and Table 3

Reference the Plots

Code
You can reference the plots the same way you do the figures above like this:
`r fig_nums("a_plot", display = "cite")`,
Result

You can reference the plots the same way you do the figures above like this: Figure 4,

Formulas

Here is how to reference Mathjax formulas. The idea is that you label the equation with an latex \label{} and then reference that label using \eqref{}. You need to either enable automatic Mathjax formula numbering or you can add a \tag{} to create a custom tag for the equation.

Reference the Equation

Code
In equation $\eqref{eq:sample}$, we find the value of an interesting integral:
Result

In equation \(\eqref{eq:sample}\), we find the value of an interesting integral:

Create the Equation

Code
$$
\int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15}
\label{eq:sample}
\tag{3} % Optional, only if you are not using or want to override the automatic numbering.
$$
Result

\[ \int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15} \label{eq:sample} \tag{3} % Optional, only if you are not using or want to override the automatic numbering. \]