PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How to add three side by side images in R markdown each with a different caption label

 July 28, 2022     image, r-markdown     No comments   

Issue

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


Solution

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



Answered By - shafee
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing