R Plots Side By Side

  1. R makes it easy to combine multiple plots into one overall graph, using either the par or layout function. With the par function, you can include the option mfrow=c (nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. Mfcol=c (nrows, ncols) fills in the matrix by columns.
  2. Two R plot side by side in.Rmd-Files 2017-10-12. I kept wondering who to plot two R plots side by side (ie., in one “row”) in a.Rmd chunk. Here’s a way, well.
  3. Any ggplots side-by-side (or n plots on a grid) The function grid.arrange in the gridExtra package will combine multiple plots; this is how you put two side by side. Require (gridExtra) plot1.

Use the par function and set the mfrow parameter to create a side-by-side plot array with 1 row and 2 columns.; Use the plot function to create a scatterplot of the variables brain versus body from this data frame, without specifying additional arguments.

I kept wondering who to plot two R plots side by side (ie., in one “row”) in a .Rmd chunk. Here’s a way, well actually a number of ways, some good, some … not.

Side

Say, you have two plots from ggplot2, and you would like them to put them next to each other, side by side (not underneath each other):

Plotmaster Craigslist

So, grid.arrange is the key.

The code of this function was inspired by code from Benfrom this SO post.

Now, let’s load two pngs and then call the function above.

This works, it produces two plots from png files side by side.

But what about the standard knitr way?

<img src=”“https://sebastiansauer.github.io/images/2017-10-12/img1.png” title=”plot of chunk unnamed-chunk-4” alt=”plot of chunk unnamed-chunk-4” width=”30%” style=”display: block; margin: auto;” /><img src=”“https://sebastiansauer.github.io/images/2017-10-12/img2.png” title=”plot of chunk unnamed-chunk-4” alt=”plot of chunk unnamed-chunk-4” width=”30%” style=”display: block; margin: auto;” />

Does not work.

Maybe with only one value for out.width??

Nope. Does not work.

Does not work either, despite some saying so.

Maybe two times include_graphics?

No avail. Looking at the html code in the md-file which is produced by the knitr -call shows one interesting point: all this version of include_graphics produce the same code. And all have this style='display: block; margin: auto;' part in it. That obviously created problems. I am unsure who to convince include_graphics to divorce from this argument. I tried some versions of the chunk argument fig.show = hold, but to no avail.

Try this code![](https://sebastiansauer.github.io/images/2017-10-12/img1.png){ width=30% } ![](https://sebastiansauer.github.io/images/2017-10-12/img2.png){ width=40% }The two commands ![]... need not appear in one row. However, no new paragraph may separate them (no blank line between, otherwise the images will appear one below the other).

{ width=30% }{ width=40% }

Works. But the markdown way does not give the fill comfort and power. So, that’s not quite perfect.

A partial solution is there; but it’s not optimal. There wil most probably be different alternatives. For example, using plain html or Latex. But it’s a kind of pity, the include_graphics call does not work as expected (by me).

3 min read2017-10-12

I kept wondering who to plot two R plots side by side (ie., in one “row”) in a .Rmd chunk. Here’s a way, well actually a number of ways, some good, some … not.

R Show Plots Side By Side

Say, you have two plots from ggplot2, and you would like them to put them next to each other, side by side (not underneath each other):

R Plots Side By Side

So, grid.arrange is the key.

The code of this function was inspired by code from Benfrom this SO post.

Now, let’s load two pngs and then call the function above.

This works, it produces two plots from png files side by side.

R show plots side by side

But what about the standard knitr way?

Does not work.

Maybe with only one value for out.width??

Nope. Does not work.

Does not work either, despite some saying so.

Maybe two times include_graphics?

No avail. Looking at the html code in the md-file which is produced by the knitr -call shows one interesting point: all this version of include_graphics produce the same code. And all have this style='display: block; margin: auto;' part in it. That obviously created problems. I am unsure who to convince include_graphics to divorce from this argument. I tried some versions of the chunk argument fig.show = hold, but to no avail.

R Plots Side By Side

Try this code

The two commands ![]... need not appear in one row. However, no new paragraph may separate them (no blank line between, otherwise the images will appear one below the other).

{ width=30% }{ width=40% }

Works. But the markdown way does not give the fill comfort and power. So, that’s not quite perfect.

Two Plots Side By Side In R

R Plots Side By Side

Whitetail Institute Imperial No Plow

A partial solution is there; but it’s not optimal. There wil most probably be different alternatives. For example, using plain html or Latex. But it’s a kind of pity, the include_graphics call does not work as expected (by me).