insert side by side png images using knitr

30,809

Solution 1

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

Solution 2

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

Solution 3

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)

Solution 4

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.

Share:
30,809
Salvador
Author by

Salvador

Updated on July 09, 2022

Comments

  • Salvador
    Salvador almost 2 years

    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)
      ```
    
  • Climbs_lika_Spyder
    Climbs_lika_Spyder over 7 years
    markdown syntax does not allow for adjusting the image size
  • Climbs_lika_Spyder
    Climbs_lika_Spyder over 7 years
    Worked great for pdf output as well!
  • Yihui Xie
    Yihui Xie over 7 years
    You can use knitr::include_graphics() if you need more sophisticated features in knitr.
  • DHW
    DHW over 5 years
    As of now, there is markdown syntax for image size adjustment: pandoc.org/MANUAL.html#images
  • Naveen Gabriel
    Naveen Gabriel about 5 years
    But, it wont print the comment in pdf.
  • julianstanley
    julianstanley about 5 years
    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.
  • Lionel Trebuchon
    Lionel Trebuchon about 5 years
    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.
  • ACG
    ACG about 5 years
    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")))
  • invictus
    invictus about 4 years
    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))
  • EA304GT
    EA304GT about 4 years
    Also worth reading the corresponding bookdown documentation
  • Saren Tasciyan
    Saren Tasciyan over 3 years
    How to put 2 images side by side but both with different height/width?
  • Leonardo Donato Nunes
    Leonardo Donato Nunes about 3 years
    To adjust the image size you can use ![](path/to/picture.png) {width=110px}
  • Adel
    Adel almost 3 years
    What is rasterGrob doing? What is the difference with img <- readPNG?