使用 knitr::include_graphics() 在单个块中包含多个图形,每个图形都有自己的标签A、B、C、...

问题描述

我想在编译为 PDF 和 HTML 的 Rmarkdown 报告中并排包含几个 jpg/png 图形,每个图形都用一个字母(A、B、C、...)标记 .

enter image description here

使用 knitr::include_graphics() 并排包含数字很容易,并为 out.width='100%/N' 数字设置 N

```{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
```

但是,include_graphics() 不提供用字母(或偶数)标记每个数字。
这可以通过 cowplotggpubr 轻松实现,如下所示:

```{r foo-label,fig.scap='(ref:foo-scaption)'}

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

fig1_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,labels = "AUTO",# add tag label to each plot
                            align = c("hv") #"none" = default
                            )
ggarrange_plot
```

问题是生成的 PDF 文件knitr::include_graphics() 版本大 20 倍。 有谁知道如何使用 knitr::include_graphics() 将多个图形并排包含在一个块中,并且每个图形都标有一个字母?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)