[英]RMarkdown Officedown: Insert two images side by side to word document with captions
如何將兩個圖像並排插入到帶有標題的 Word 文檔 (.docx) 中?
我為 HTML 和 pdf 文檔找到了幾種解決方案。 但是,對於 word 文檔,情況似乎並非如此。
這是我的 YAML:
---
title: "Two images side by side"
output: officedown::rdocx_document
---
我知道下面的代碼一般可以並排插入兩張圖片(不帶標題):
![](path to my picture/picture.jpg){width=300} ![](path to my picture/picture.jpg){width=300}
問題是我需要通常使用 include_graphics() 的圖形標題:
```{r, fig.cap = c("cap1", "cap2")}
include_graphics(c(" to my picture/picture.jpg", " to my picture/picture.jpg"))
``
對於 HTML 和 pdf 文檔,我會將 fig.show = "hold" 添加到塊中以將它們並排堆疊。 但是,對於 word 文檔,這不會改變任何東西。
由於還沒有人發表評論,我只是指出,作為一種解決方法,您可以使用兩列布局(您還必須加載officer
包以進行對齊)。 將帶有標題的圖放在單獨的列中(如果它們具有相同的高度,這很好用),或者只是在並排圖下方添加標題,例如在第一個場景中如下所示:
<!---BLOCK_MULTICOL_START--->
![Caption 1](picture1.jpg){width=300}<caption>
![Caption 2](picture2.jpg){width=300}<caption>
<!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->
這也有效:
<!---BLOCK_MULTICOL_START--->
![Caption 1](picture1.jpg){width=300} <caption> ![Caption 2](picture2.jpg){width=300}
<!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->
更多的解決方法是,對於具有不同高度的圖像,您可以通過在圖像下方打開一個兩列部分並手動添加標題文本來保持標題的垂直位置不變:
![](picture1.jpg){width=300} ![](picture2.jpg){width=300}
<!---BLOCK_MULTICOL_START--->
Caption 1`r fp_par(text.align = "center")`
<!---CHUNK_COLUMNBREAK--->
Caption 2`r fp_par(text.align = "center")`
<!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->
非常感謝,這完美!
我什至可以在您的解決方案中使用include_graphics()
,這很好,因為它允許更多圖像規范。
<!---BLOCK_MULTICOL_START--->
[```]{r, fig.cap = "cap1", fig.id = "id1", fig.dim = c(length, height)}
include_graphics("path to my picture/picture.jpg")
[```]
[```]{r, fig.cap = "cap2", fig.id = "id2", fig.dim = c(length, height)}
include_graphics("path to my picture/picture.jpg")
[```]
<!---BLOCK_MULTICOL_STOP{widths: [3.6,3.6]}--->
另一種方法是使用ggplot2
和cowplot
包。 cowplot
需要安裝magick
,但不一定要加載。
一個類似於RStudio 社區帖子的示例,但具有不同的圖像:
library(cowplot)
library(ggplot2)
path1 <- "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png"
path2 <- "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png" # in this case, the image is identical
plot1 <- ggdraw() + draw_image(path1)
plot2 <- ggdraw() + draw_image(path2)
plot_grid(plot1, plot2) # output both plots
一個更復雜的版本包含在另一個SO question中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.