3

I would like to include several jpg/png figures side by side and each labeled by a letter (A, B, C, ...) in a Rmarkdown report compiled to both PDF and HTML.

enter image description here

Including the figures side by side is easy with knitr::include_graphics() and setting out.width='100%/N' for N figures:

```{r foo-label, echo=FALSE, out.width='100%', fig.align = "center", fig.cap='(ref:foo-caption)', fig.scap='(ref:foo-scaption)'}
fig1_path <- paste0(fig_path,"Fig1.jpg")
fig2_path <- paste0(fig_path,"Fig2.jpg")
fig3_path <- paste0(fig_path,"Fig3.jpg")

knitr::include_graphics(c(fig1_path, fig2_path, fig3_path), auto_pdf = TRUE)
# if auto_pdf = TRUE: includes PDF version of figure if available in same folder
```

However, include_graphics() does not offer to label each figure by a letter (or even number).
This can easily be achieved with cowplot or ggpubr as follows:

```{r foo-label, echo=FALSE, out.width='100%', fig.align = "center", fig.cap='(ref:foo-caption)', fig.scap='(ref:foo-scaption)'}

library(cowplot) # for ggdraw() & draw_image()
library(ggpubr)
library(ggplot2)

fig1_path <- paste0(fig_path,"Fig1.jpg")
fig2_path <- paste0(fig_path,"Fig2.jpg")
fig3_path <- paste0(fig_path,"Fig3.jpg")

# scale down all figures proportionally size to increase space between them
scaling <- 0.9
fig1 <- ggdraw() + draw_image(fig1_path, scale = scaling)
fig2 <- ggdraw() + draw_image(fig2_path, scale = scaling)
fig3 <- ggdraw() + draw_image(fig3_path, scale = scaling)

# cowplot style:
cowplot_plot <- plot_grid(fig1, fig2, fig3, labels = "AUTO")
cowplot_plot

# ggpubr style:
ggarrange_plot <- ggarrange(fig1, fig2, fig3,
                            labels = "AUTO", # add tag label to each plot
                            align = c("hv") #"none" = default
                            )
ggarrange_plot
```

The problem is that the resulting PDF file is 20x larger than the knitr::include_graphics() version. Would anyone know how to use knitr::include_graphics() to include multiple figures side by side in a single chunk and each figure labeled with a letter?

mavericks
  • 1,005
  • 17
  • 42
  • How were the images created in the first place? If you have the control, it might be best to do all the labeling during that process. – Tung Dec 26 '20 at 17:12
  • Images are of various types (photographs, digital/scanned sketches, ...). If feasible I would like to refrain from the option to add the labels in another program manually. Also, when the figure order is changed, all labels require manual updating in this case. – mavericks Dec 27 '20 at 08:41
  • 1
    Sub-figures are supported only in PDF output: https://bookdown.org/yihui/rmarkdown-cookbook/latex-subfigure.html – Yihui Xie Dec 29 '20 at 19:09
  • @YihuiXie Thank you for the input. Going as far as a subfigure with a separate caption is not even needed, a simple "A", "B" and "C" next to the top of each included figure would do, see the sketch added. – mavericks Dec 30 '20 at 15:50

0 Answers0