Home > database >  How to add three side by side images in R markdown each with a different caption label
How to add three side by side images in R markdown each with a different caption label

Time:07-27

I am writing a report by Rmarkdown. I want to have three images next to each other with different caption labels .

this is my code :

library(png)
library(grid)
library(gridExtra)
img1 <-  rasterGrob(as.raster(readPNG("~/Desktop/GIS/project/jpg/yek.png")), interpolate = FALSE)
img2 <-  rasterGrob(as.raster(readPNG("~/Desktop/GIS/project/jpg/do.png")), interpolate = FALSE)
grid.arrange(img1, img2, ncol = 2)


the problem is that there is no empty space between images and I don't have separate captions for each of them

thank you in advance for your help

CodePudding user response:

Using latex package subfig, you can do this.

---
title: "Untitled"
author: "None"
date: '2022-07-26'
output:
  pdf_document:
    extra_dependencies: "subfig"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}
#| fig.cap = "",
#| fig.subcap = c('Image 1', 'Image 2', 'Image 3'),
#| fig.ncol = 3,
#| out.width = "32%",
#| fig.align = "center"

library(png)
library(grid)

img1 <- rasterGrob(as.raster(readPNG("test.png")), interpolate = FALSE)
img2 <- rasterGrob(as.raster(readPNG("test.png")), interpolate = FALSE)
img3 <- rasterGrob(as.raster(readPNG("test.png")), interpolate = FALSE)

plot(img1)
plot(img2)
plot(img3)
```

side by side image

  • Related