如何将两个图像并排插入到带有标题的word文档(.docx)中?
我找到了HTML和pdf文档的几种解决方案。然而,对于word文档,情况似乎并非如此。
这是我的YAML:
---
title: "Two images side by side"
output: officedown::rdocx_document
---我知道以下代码通常可以并排插入两个图像(没有标题):
{width=300} {width=300}这方面的问题是,我需要图形标题,通常对其使用include_graphics():
```{r, fig.cap = c("cap1", "cap2")}include_graphics(c(“到我的图片/picture.jpg”,“到我的图片/picture.jpg”))
对于HTML文档,我会将fig.show = "hold“添加到块中,将它们并排堆叠起来。然而,对于word文档,这不会改变任何事情。
发布于 2022-03-09 11:56:52
由于还没有人发表评论,我只是指出,作为一种解决办法,您可以使用两列布局(您还必须加载officer包以进行对齐)。或者将带有标题的图形放在单独的列中(如果它们具有相同的高度,这是很好的),或者将标题添加到并排图下面,例如在第一个场景中:
<!---BLOCK_MULTICOL_START--->
{width=300}<caption>
{width=300}<caption>
<!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->这也适用于:
<!---BLOCK_MULTICOL_START--->
{width=300} <caption> {width=300}
<!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->更重要的是,对于不同高度的图像,您可以通过打开图像下的两列部分并手动添加标题文本来保持标题的垂直位置不变:
{width=300} {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}--->发布于 2022-03-11 18:09:10
非常感谢,这是完美的!
我甚至可以在您的解决方案中使用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]}--->发布于 2022-06-02 13:49:29
另一种方法是使用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另一个所以问题中包含了一个更复杂的版本。
https://stackoverflow.com/questions/71394122
复制相似问题