【问题标题】:Put image objects side by side in Rmarkdown将图像对象并排放置在 Rmarkdown 中
【发布时间】:2016-06-30 02:13:16
【问题描述】:

有没有办法可以在 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 的内容,只有一些其他关于保存图像的建议,而不是我在这里展示的对象。

【问题讨论】:

    标签: r r-markdown


    【解决方案1】:

    这是我如何安排三个地块

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

    结果是这样的

    【讨论】:

    • 效果很好,谢谢。你知道是否有任何方法可以用 R 中的 html 小部件做类似的事情?喜欢dygraphs 包中的一个?它说只允许grobs
    猜你喜欢
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2022-12-11
    相关资源
    最近更新 更多