knitr:如何显示彼此相邻的两个不同大小的图?

时间:2022-10-15 19:14:16

I wanted to generate two images of different sizes, but show them side-by-side. Is this possible?

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

This works, but then they have to be the same size:

这有效,但是它们必须是相同的大小:

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

This doesn't work, but it could since in Markdown, lines that are separated by a single newline, appear on the same line.

这不起作用,但是因为在Markdown中,由单个换行符分隔的行显示在同一行上。

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

4 个解决方案

#1


7  

Another option, if you're outputting to HTML is to use the out.extra= chunk option, and set them to be float objects within a block. For example.

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

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

#2


9  

One option would be to make a single wide graph with the R commands and give knitr just the one graph to deal with, maybe something like:

一种选择是用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


6  

You can also use grid.arrange from gridExtra which works with grob or ggplot objects

您还可以使用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)

#4


5  

Yet another option is to use a vector for out.width or out.height, if you do not mind resizing the plots, e.g.

另一个选择是使用向量输出out.width或out.height,如果你不介意调整大小,例如

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

#1


7  

Another option, if you're outputting to HTML is to use the out.extra= chunk option, and set them to be float objects within a block. For example.

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

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

#2


9  

One option would be to make a single wide graph with the R commands and give knitr just the one graph to deal with, maybe something like:

一种选择是用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


6  

You can also use grid.arrange from gridExtra which works with grob or ggplot objects

您还可以使用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)

#4


5  

Yet another option is to use a vector for out.width or out.height, if you do not mind resizing the plots, e.g.

另一个选择是使用向量输出out.width或out.height,如果你不介意调整大小,例如

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