insert side by side png images using knitr
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.
Salvador
Updated on July 09, 2022Comments
-
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 over 7 yearsmarkdown syntax does not allow for adjusting the image size
-
Climbs_lika_Spyder over 7 yearsWorked great for pdf output as well!
-
Yihui Xie over 7 yearsYou can use
knitr::include_graphics()
if you need more sophisticated features in knitr. -
DHW over 5 yearsAs of now, there is markdown syntax for image size adjustment: pandoc.org/MANUAL.html#images
-
Naveen Gabriel about 5 yearsBut, it wont print the comment in pdf.
-
julianstanley about 5 yearsThere 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 about 5 yearsAwesome! 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 about 5 yearsAnd 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 about 4 yearsthis 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 about 4 yearsAlso worth reading the corresponding bookdown documentation
-
Saren Tasciyan over 3 yearsHow to put 2 images side by side but both with different height/width?
-
Leonardo Donato Nunes about 3 yearsTo adjust the image size you can use
![](path/to/picture.png) {width=110px}
-
Adel almost 3 yearsWhat is rasterGrob doing? What is the difference with img <- readPNG?