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

Basic Boxplot in R. Figure 1 visualizes the output of the boxplot command: A box-and-whisker plot. Stacked Barplots with ggplot2 in R Stacked Barplot Side By Side with position=”dodge” Barplots stacked vertically are often harder to interpret, as it is harder to make comparison with one main group to another. A better solution is to make the grouped barplots such that bars are located side-by-side. Side-By-Side Horizontal Boxplot Using ggplot2 You can rotate the previously created plot by adding the coordflip arguement. Side-By-Side Horizontal Boxplot Using ggplot2 You can rotate the previously created plot by adding the coordflip arguement. My.bp rotates the boxplot my.bp Mathematicss, Computer Science, and Statistics Department Gustavus Adolphus College.

Side By Side Barplot In R

This video explains how to plot two ggplots in the same plot window side-by-side. More info: https://statisticsglobe.com/draw-multiple-ggplot-plots-side-by-s.

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?

R ggplot multiple plots

<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;” />

Markdown

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% }

Whitetail Institute Imperial No Plow

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

R Two Plots Side By Side Ggplot

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