在 Rmarkdown 中并排放置图像对象
Put image objects side by side in Rmarkdown
有没有办法在 Rmarkdown 中并排放置两个图像对象(html 输出)?
理想情况下,我希望能够做这样的事情:
```{r}
library(ggplot2)
library(data.table)
dt<-data.table(a=1:10, b=1:10)
gg <- ggplot(dt,aes(a,b)) + geom_line()
```
然后在另一个代码块或类似内联中调用 gg
两次(或使用另一个对象)(尽管这不起作用)
`r gg` `r gg`
并让它们并排出现在 html 中。我没有看到任何特定于 Rmarkdown 的东西,只有一些其他关于保存图像的建议,而不是我在这里展示的对象。
我是这样安排三个地块的
library(ggplot2)
library(gridExtra)
o1 <- ggplot(...)
o2 <- ggplot(...)
o3 <- ggplot(...)
# for Aligning Axes in ggplot2 see
# http://www.exegetic.biz/blog/2015/05/r-recipe-aligning-axes-in-ggplot2/
o2 <- ggplot_gtable(ggplot_build(o2))
o3 <- ggplot_gtable(ggplot_build(o3))
maxWidth = unit.pmax(o2$widths[2:3], o3$widths[2:3])
o2$widths[2:3] <- maxWidth
o3$widths[2:3] <- maxWidth
grid.arrange(o1, arrangeGrob(o2, o3, nrow=2),
ncol=2, widths=c(1, 2))
结果是这样的
有没有办法在 Rmarkdown 中并排放置两个图像对象(html 输出)?
理想情况下,我希望能够做这样的事情:
```{r}
library(ggplot2)
library(data.table)
dt<-data.table(a=1:10, b=1:10)
gg <- ggplot(dt,aes(a,b)) + geom_line()
```
然后在另一个代码块或类似内联中调用 gg
两次(或使用另一个对象)(尽管这不起作用)
`r gg` `r gg`
并让它们并排出现在 html 中。我没有看到任何特定于 Rmarkdown 的东西,只有一些其他关于保存图像的建议,而不是我在这里展示的对象。
我是这样安排三个地块的
library(ggplot2)
library(gridExtra)
o1 <- ggplot(...)
o2 <- ggplot(...)
o3 <- ggplot(...)
# for Aligning Axes in ggplot2 see
# http://www.exegetic.biz/blog/2015/05/r-recipe-aligning-axes-in-ggplot2/
o2 <- ggplot_gtable(ggplot_build(o2))
o3 <- ggplot_gtable(ggplot_build(o3))
maxWidth = unit.pmax(o2$widths[2:3], o3$widths[2:3])
o2$widths[2:3] <- maxWidth
o3$widths[2:3] <- maxWidth
grid.arrange(o1, arrangeGrob(o2, o3, nrow=2),
ncol=2, widths=c(1, 2))
结果是这样的