Search code examples
rknitrr-markdownbookdown

Image footnotes in Bookdown


How can I add a figure note right below the image in bookdown::pdf_document2? It is relatively easy to do for plots that created by ggplot2. grid.arrange can do the work with grid.arrange(<plot>, bottom = <figure_notes>). But if I insert an image manually, e.g., knitr::include_graphics(rep("images/knit-logo.png", 3)), is there a way to add a figure note to it? I saw a knitr option, fig.subcap. But this fails to compile:

```{r fig.show="hold", fig.cap = "a", fig.subcap = "b"}

knitr::include_graphics(rep("images/knit-logo.png", 3))

```

This returns:

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6350 64-bit)
entering extended mode
! Undefined control sequence.
<recently read> \subfloat 

Error: Failed to compile miniSample.tex. See miniSample.log for more info.

Thanks


Solution

  • Found an expedient solution:

    1. Read the image into R using magick::image_read;
    2. Convert the image to raster project with rasterGrob;
    3. Create a note with textGrob; Use grid.arrange to present;
    4. Present the image together with note using grid.arrange.

      plot_A <- magick::image_read(<path>) %>% rasterGrob(interpolate = TRUE) note <- textGrob("This is a note.") grid.arrange(plot_A, bottom = note)