sammo3182
sammo3182

Reputation: 163

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

Upvotes: 1

Views: 1343

Answers (2)

Michael Harper
Michael Harper

Reputation: 15369

If you want to use subfigures, you have to load the subfig package within LaTeX. This can be done by using the header-includes argument in the YAML, as explained here.

Here is a minimal, reproducible example, It creates a local image "temp.jpg" and then produces three subfigures:

---
output: pdf_document
header-includes:
   - \usepackage{subfig}
---

```{r}
jpeg(filename = "temp.jpg")
plot(cars)
dev.off()
```

```{r fig.show="hold", fig.cap = "a", fig.subcap = c("Your Caption", "Another Caption", "Isn't this great"), fig.ncol = 3, out.width="33%"}
knitr::include_graphics(rep("temp.jpg", 3))
```

enter image description here

Check out this post for the original description: Subfigures or Subcaptions with knitr?

Upvotes: 1

sammo3182
sammo3182

Reputation: 163

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)

Upvotes: 0

Related Questions