I want to add two images side by side in R markdown, but with some space between them. How can I adjust the space between the images? Thank you!
![](/Users/filename/black.png){width=50%}
![](/Users/filename/gray.png){width=50%}
CodePudding user response:
One option could be using ggdraw() draw_image
from {cowplot}
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r side_by_side, fig.align='center'}
library(cowplot)
ggdraw()
draw_image("test_logo.png", width = 0.4)
draw_image("test_animal.jpg", width = 0.4, x = 0.5)
```
Which looks like
Another option could be making white border for the images using image_border
from the {magick}
package so that even if there is no actual space between them, white border will look like a space.
```{r another_option, warning=FALSE, message=FALSE}
library(magick)
img1 <- image_read("test_logo.png")
bordered_img1 <- image_border(img1, "white", "20x20")
img2 <- image_read("test_animal.jpg")
bordered_img2 <- image_border(img2, "white", "20x20")
image_write(bordered_img1, "img1.png")
image_write(bordered_img2, "img2.jpg")
```
```{r side_img, out.width="50%"}
knitr::include_graphics(c("img1.png", "img2.jpg"))
```
Which looks like,