44

How can I insert side by side png files from my computer into rstudio when creating an html document?

The following works well (plots)

```{r, echo=FALSE,fig.width=4, fig.show='hold'}
 plot(cars)
plot(rnorm(100))
```

But for images from a path, only the last image is displayed

 ```{r fig.width=3, fig.show='hold'}
   library(png)
  img <- readPNG("C:/path to my picture/picture.png")
  grid.raster(img)

  img2 <- readPNG("C:/path to my picture/picture2.png")
  grid.raster(img2)
  ```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Salvador
  • 1,229
  • 1
  • 11
  • 19

4 Answers4

40

You can use knitr::include_graphics() as this one accepts a vector of paths as an argument.

Then you should use fig.show='hold',fig.align='center' in order to plot them on the same line and out.width="49%", out.height="20%" to control the output size.

```{r, echo=FALSE,out.width="49%", 
out.height="20%",fig.cap="caption",fig.show='hold',fig.align='center'}
knitr::include_graphics(c("path/to/img1","path/to/img1"))
``` 
alexwhan
  • 15,636
  • 5
  • 52
  • 66
Marc Bataillou
  • 608
  • 6
  • 4
  • 1
    There it is. Worked perfectly with HTML output, R 3.5.2, knitr 1.21. Thanks for taking the time to answer an old question. – julianstanley Feb 25 '19 at 17:58
  • Awesome! I can confirm that this works with PDF output too, R 3.5.2, knitr 1.21. Now I am just wondering how to allow for a combined out.width bigger than 100%. Haven't found a solution so far. – Lionel Trebuchon Mar 20 '19 at 10:52
  • 2
    And if you're using the here package (you should be using the here package) it's knitr::include_graphics(here("path", "to", c("img1","img2"))) – ACG Mar 30 '19 at 21:36
  • this isn't working for me. I have: `knitr::include_graphics(c("a.gif","b.gif"))`. Paths are correct (it works if I do `![](a.gif)`) – invictus Apr 02 '20 at 13:40
  • Also worth reading the [corresponding bookdown documentation](https://bookdown.org/yihui/bookdown/figures.html#fnref6) – EA304GT May 04 '20 at 20:59
  • How to put 2 images side by side but both with different height/width? – Saren Tasciyan Nov 25 '20 at 14:20
  • This is only controlling the size of the first image, not the second. has knitr or rmd changed? (Nevermind, this was happening because my original images are different sizes) – missgwolf Jan 21 '23 at 07:02
31

You should learn the syntax of Markdown (really, you need about five minutes). The solution does not even involve R at all:

![](path/to/picture.png) ![](path/to/picture2.png)

BTW, you'd better avoid absolute paths. Use relative paths (relative to your Rmd file).

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
24

We still lack a good answer to this question if the desired output is a MS Word document (I see that the OP specifically asked for HTML output, but I'm guessing I'm not the only one who came here looking for a solution that works for MS Word docs also).

Here's one method, based on this and this, but the result is not very satisfactory:

library(png)
library(grid)
library(gridExtra)
img1 <-  rasterGrob(as.raster(readPNG("path/to/picture1.png")), interpolate = FALSE)
img2 <-  rasterGrob(as.raster(readPNG("path/to/picture2.png")), interpolate = FALSE)
grid.arrange(img1, img2, ncol = 2)
Bolein95
  • 2,947
  • 2
  • 26
  • 31
Ben
  • 41,615
  • 18
  • 132
  • 227
8

One can also use cowplot:

library(cowplot)
ggdraw() + 
  draw_image("path/to/picture1.png", width = 0.5) + 
  draw_image("path/to/picture2.png", width = 0.5, x = 0.5)

Should work for all output formats as well.

M. Rodo
  • 448
  • 4
  • 12
  • This really is the best option I could find so far when working with MS Word output. Yet, I still don't have an option like "out.width='100%'" so I have to set "fig.width=6" which is roughly the A4 page width and then play around with "fig.height" until it works. – Paul Schmidt Jul 28 '22 at 10:04