2

How can I insert two images side by side into a word document (.docx) with captions?

I found several solutions for HTML and pdf documents. However, for word documents, this doesn't seem to be the case.

This is my YAML:

---
title: "Two images side by side"
output: officedown::rdocx_document
---

I know that the following code can generally insert two images side by side (without captions):

![](path to my picture/picture.jpg){width=300} ![](path to my picture/picture.jpg){width=300}

The problem with this is that I need figure captions for which I normally use include_graphics():

```{r, fig.cap = c("cap1", "cap2")}
include_graphics(c(" to my picture/picture.jpg", " to my picture/picture.jpg"))
`` 

For HTML and pdf documents I would add fig.show = "hold" to the chunk to stack them side by side. However, for word documents, this doesn't change anything.

3 Answers3

2

Since no one commented yet, I am just pointing out that you could, as a workaround, use a two column layout (you have to load the officer package as well for the alignment). Either put the graphs with caption in separate columns (if they have the same height this works well), or just add the captions below the side-by-side graph, e.g. like this in the first scenario:

<!---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}--->

This also works:

<!---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}--->

Even more of a workaround, for images with different heights, you can keep the vertical position of the captions constant by opening a two-column section under the images and manually adding the caption text:

![](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}--->

user12728748
  • 8,106
  • 2
  • 9
  • 14
0

Thanks a lot, this works perfectly!

I am even able to use include_graphics() within your solution, which is nice since it allows for more image specifications.

<!---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]}--->
0

Another approach is with the ggplot2 and cowplot packages. cowplot requires magick to be installed but not necessarily loaded.

An example similar to a RStudio community post, but with different images:

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

A more complex version is included in another SO question.

MBorg
  • 1,345
  • 2
  • 19
  • 38