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)
  ```

4 Answers 4

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"))
``` 
7
  • 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. Feb 25, 2019 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. Mar 20, 2019 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, 2019 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 2, 2020 at 13:40
  • Also worth reading the corresponding bookdown documentation
    – EA304GT
    May 4, 2020 at 20:59
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).

8
  • 3
    markdown syntax does not allow for adjusting the image size Nov 18, 2016 at 21:51
  • 7
    You can use knitr::include_graphics() if you need more sophisticated features in knitr.
    – Yihui Xie
    Nov 18, 2016 at 22:31
  • 1
    As of now, there is markdown syntax for image size adjustment: pandoc.org/MANUAL.html#images
    – DHW
    Feb 3, 2019 at 16:39
  • But, it wont print the comment in pdf. Feb 22, 2019 at 14:09
  • To adjust the image size you can use ![](path/to/picture.png) {width=110px} Feb 24, 2021 at 20:42
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)
3
  • Worked great for pdf output as well! Nov 18, 2016 at 21:53
  • What is rasterGrob doing? What is the difference with img <- readPNG?
    – Adel
    Jul 30, 2021 at 21:20
  • It works well, But how can have a small space between the two images and how can have different captions for each image ? Jul 24, 2022 at 10:40
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.

1
  • 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. Jul 28, 2022 at 10:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.