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.
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?