Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knitr: How to show two plots of different sizes next to each other?

Tags:

r

knitr

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.

```{r normal_plot, fig.width=5, fig.height=5}
    plot(...)
```
```{r tall_plot, fig.width=5, fig.height=9}
    plot(...)
```
like image 540
nachocab Avatar asked Dec 21 '12 20:12

nachocab


2 Answers

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.

```{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)
```
like image 137
Brandon Bertelsen Avatar answered Oct 21 '22 18:10

Brandon Bertelsen


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 fig.width=10, fig.height=9}
layout( cbind( c(0,0,1,1,1,1,1,0,0), rep(2,9) ) )
plot(...)
plot(...)
```
like image 36
Greg Snow Avatar answered Oct 21 '22 17:10

Greg Snow