Figure Captions, References Using Knitr and Markdown to HTML

figure captions, references using knitr and markdown to html

  1. You can create the figure numbers with a simple counter in R; see one example here. The problem is whether the markdown renderer will render the figure caption for you: R Markdown v1 won't, but v2 (based on Pandoc) will.
  2. I do not know. There is no direct way to insert a label as an identifier for figures, so it is probably not possible to cross reference figures with pure Markdown. Once you've got issues like this, think (1) do I really need it? (2) if it is intended to be a document with a complicated structure, I think it is better to use LaTeX directly (Rnw documents).

Numbered captions on customized and reactive figure in R markdown HTML file

In order to address this issue, it is important to know that knitr uses #fig:label in pandoc in order to create the automated numbering in the output. You can see this when you examine the markdown temporary output from the knitr process:

<div class="figure">
<img src="web_tiles_v2_files/figure-html/otherchunk-1.png" alt="SECOND FIGURE CAP" />
<p class="caption">(\#fig:SPECIFICCHUNKOFINTEREST)SECOND FIGURE CAP</p>
</div>

As such, one should be able to maintain the customized figure output by adjusting the custom hook seen in other solutions. This would look like the following, just make sure you have the right chunk label included:

```{r}
knit_hooks$set(customcap= function(before, options, envir) {
if(!before) {
paste('<p class="caption"> (\\#fig:chunk1)',options$customcap,"</p>",sep="")
}
})
```

One could also use some of the internal knitr functions to automatically grab the fig.lp and label: paste('<p class="caption">', knitr:::create_label(options$fig.lp, options$label), options$customcap,"</p>", sep="")

The entire code would look like:

---
output:
bookdown::html_document2:
self-contained: TRUE
---

```{css, echo=FALSE}
#dualpanel {
width: 50%
}

@media screen and (max-width: 500px) {
#dualpanel {
width: 100%
}}
```

```{r}
knit_hooks$set(customcap= function(before, options, envir) {
if(!before) {
paste('<p class="caption"> (\\#fig:chunk1)',options$customcap,"</p>",sep="")
}
})
```

```{r chunk1, echo=FALSE, customcap='FIRST FIGURE CAP'}
temp <- plotly::plot_ly(mtcars, x = ~cyl, y=~mpg)
shiny::div(class = 'figure',
style = "display: flex; flex-wrap: wrap; justify-content: center",
shiny::div(temp, id = 'dualpanel'),
shiny::div(temp, id = 'dualpanel'))
```

```{r chunk2, echo=FALSE, fig.cap='SECOND FIGURE CAP'}
plot(mtcars$cyl, mtcars$mpg)
```

This produces an output that has the dynamic elements and maintains labeling.

Sample Image

Sample Image

knitr: can I cite an article in a figure caption using the fig.cap chunk option?

The bookdown package extends the functionality of rmarkdown and provides some useful tools. Text-references can be used to address this problem. As described by the package author, text references can be used:

You can assign some text to a label and reference the text using the
label elsewhere in your document.

This works really well with citations, as shown below:

---
output: bookdown::tufte_handout2
references:
- id: Nobody06
title: 'My Article'
author:
- family: Nobody
given: Jr
issued:
year: 2006
---

(ref:crossref) Some text [@Nobody06].

```{r figure, fig.cap="(ref:crossref)"}
library(ggplot2)
qplot(1:10, rnorm(10))
```

# References

Sample Image

You'll notice that the output format has been adjusted to bookdown::tufte_handout2 which allows the bookdown features to work. You can find a full list of the output formats here.

Read more about text references here: https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#text-references

Figures captions and labels in knitr

You can achieve this by including fig_caption: yes in the header:

---
title: "Plotting"
output:
pdf_document:
fig_caption: yes
---

```{r figs, echo=FALSE, fig.width=7,fig.height=6,fig.cap="\\label{fig:figs}plotting example"}
par(mfrow=c(2,2))
plot(1:10, col=2)
plot(density(runif(100, 0.0, 1.0)))
plot(runif(100, 0.0, 1.0),type="l")
```

in Figure \ref{fig:figs} we see examples of plotting in R.

click here to see a screenshot

Note that the figure caption label should be included in the caption with a double backslash, as shown above.

How to make a figure caption in Rmarkdown?

Please see the documentation of R Markdown for PDF output, and in particular, look for fig_caption. Figure captions are turned off by default in R Markdown, and you have to turn them on (fig_caption: true). You can also find this setting from the gear button on the toolbar of RStudio IDE.



Related Topics



Leave a reply



Submit