如何解决使用 knitr::include_graphics() 在单个块中包含多个图形,每个图形都有自己的标签A、B、C、...
我想在编译为 PDF 和 HTML 的 Rmarkdown 报告中并排包含几个 jpg/png 图形,每个图形都用一个字母(A、B、C、...)标记 .
使用 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()
不提供用字母(或偶数)标记每个数字。
这可以通过 cowplot
或 ggpubr
轻松实现,如下所示:
```{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 举报,一经查实,本站将立刻删除。