Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Any ggplots side-by-side (or n plots on a grid)</h3> <p>The function <code>grid.arrange()</code> in the <a href="https://cran.r-project.org/web/packages/gridExtra/index.html" rel="noreferrer"><code>gridExtra</code></a> package will combine multiple plots; this is how you put two side by side. </p> <pre><code>require(gridExtra) plot1 &lt;- qplot(1) plot2 &lt;- qplot(1) grid.arrange(plot1, plot2, ncol=2) </code></pre> <p>This is useful when the two plots are not based on the same data, for example if you want to plot different variables without using reshape(). </p> <p>This will plot the output as a side effect. To print the side effect to a file, specify a device driver (such as <code>pdf</code>, <code>png</code>, etc), e.g.</p> <pre><code>pdf("foo.pdf") grid.arrange(plot1, plot2) dev.off() </code></pre> <p>or, use <code>arrangeGrob()</code> in combination with <code>ggsave()</code>,</p> <pre><code>ggsave("foo.pdf", arrangeGrob(plot1, plot2)) </code></pre> <p>This is the equivalent of making two distinct plots using <code>par(mfrow = c(1,2))</code>. This not only saves time arranging data, it is necessary when you want two dissimilar plots.</p> <hr> <h3>Appendix: Using Facets</h3> <p>Facets are helpful for making similar plots for different groups. This is pointed out below in many answers below, but I want to highlight this approach with examples equivalent to the above plots. </p> <pre><code>mydata &lt;- data.frame(myGroup = c('a', 'b'), myX = c(1,1)) qplot(data = mydata, x = myX, facets = ~myGroup) ggplot(data = mydata) + geom_bar(aes(myX)) + facet_wrap(~myGroup) </code></pre> <hr> <h3>Update</h3> <p>the <code>plot_grid</code> function in the <a href="https://cran.r-project.org/web/packages/cowplot/index.html" rel="noreferrer"><code>cowplot</code></a> is worth checking out as an alternative to <code>grid.arrange</code>. See the <a href="https://stackoverflow.com/a/31223588/199217">answer</a> by @claus-wilke below and <a href="https://stackoverflow.com/a/31223588/199217">this vignette</a> for an equivalent approach; but the function allows finer controls on plot location and size, based on <a href="https://stackoverflow.com/a/31223588/199217">this vignette</a>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload