【问题标题】:knitr: How to show two plots of different sizes next to each other?knitr:如何将两个不同大小的地块并排显示?
【发布时间】:2012-12-21 20:08:39
【问题描述】:

我想生成两个不同大小的图像,但并排显示它们。这可能吗?

这可行,但它们的大小必须相同:

```{r two_plots_same_size_side_by_side, fig.width=5, fig.height=5}
    plot(...)
    plot(...)
```

这不起作用,但它可以因为在 Markdown 中,由单个换行符分隔的行出现在同一行。

```{r normal_plot, fig.width=5, fig.height=5}
    plot(...)
```
```{r tall_plot, fig.width=5, fig.height=9}
    plot(...)
```

【问题讨论】:

    标签: r knitr


    【解决方案1】:

    如果您要输出到 HTML,另一个选项是使用 out.extra= 块选项,并将它们设置为块内的浮动对象。例如。

    ```{r fig.width=4, fig.height=6,echo=FALSE,out.extra='style="float:left"'}
    plot(cars)
    ```{r fig.width=8, fig.height=6,echo=FALSE, out.extra='style="float:left"'}
    plot(cars)
    ```
    

    【讨论】:

    • 这很酷。我将在第二个情节之后添加
      以防有任何文本。谢谢
    • 如果您可以选择合并原始 HTML,最好将它们放在 disply:block; 容器中。
    【解决方案2】:

    一种选择是使用 R 命令制作一个宽图,并只给 knitr 一个要处理的图,可能类似于:

    ```{r fig.width=10, fig.height=9}
    layout( cbind( c(0,0,1,1,1,1,1,0,0), rep(2,9) ) )
    plot(...)
    plot(...)
    ```
    

    【讨论】:

      【解决方案3】:

      如果您不介意调整绘图大小,例如

      ```{r out.width=c('500px', '300px'), fig.show='hold'}
      boxplot(1:10)
      plot(rnorm(10))
      ```
      

      【讨论】:

      • @nachocab 你用的是最新版的 knitr 吗?
      • 您介意为 Latex 发布相同的代码吗?谢谢。
      • @dariaa 类似<<out.width=c('3in', '5in'), fig.show='hold'>>=
      • 谢谢@Yihui。你能看看这个相关的问题stackoverflow.com/questions/31992128/… 吗?
      【解决方案4】:

      您还可以使用 gridExtra 中的 grid.arrange,它适用于 grob 或 ggplot 对象

      require(gridExtra)
      pre_fig <- rasterGrob(readPNG("paper_figures/surf_0000.png"), interpolate=TRUE)
      post_fig <- rasterGrob(readPNG("paper_figures/surf_0044.png"), interpolate=TRUE)
      grid.arrange(pre_fig, post_fig, ncol=2)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多