在创建html文档时,如何将计算机中的并排png文件插入rstudio?
以下效果很好(情节)
```{r, echo=FALSE,fig.width=4, fig.show='hold'}
plot(cars)
plot(rnorm(100))
```
Run Code Online (Sandbox Code Playgroud)
但对于路径中的图像,仅显示最后一个图像
```{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)
```
Run Code Online (Sandbox Code Playgroud)
Yih*_*Xie 19
你应该学习Markdown的语法(实际上,你需要大约五分钟).该解决方案甚至根本不涉及R:
 
Run Code Online (Sandbox Code Playgroud)
顺便说一下,你最好避免使用绝对路径.使用相对路径(相对于您的Rmd文件).
Ben*_*Ben 13
如果所需的输出是MS Word文档,我们仍然缺乏对这个问题的良好答案(我看到OP专门要求HTML输出,但我猜我不是唯一一个来到这里寻找解决方案的人也适用于MS Word文档).
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)
Run Code Online (Sandbox Code Playgroud)
小智 12
您可以使用它knitr::include_graphics()作为接受路径矢量的参数。
然后,您应该使用fig.show='hold',fig.align='center'以便将它们绘制在同一条线上并out.width="49%", out.height="20%"控制输出大小。
```{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"))
```
Run Code Online (Sandbox Code Playgroud)
还可以使用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)
Run Code Online (Sandbox Code Playgroud)
也应该适用于所有输出格式。
| 归档时间: |
|
| 查看次数: |
16655 次 |
| 最近记录: |