17

I'm printing two plots with ggplot into an R Markdown HTML output, but I'd like them to appear side by side. Is this possible? Could I set the size of the figures as well?

SO far I can only print them one after the other. I also tried the multiplot function from the R Cookbook, but that severely distorts the plots...

Thanks!


title: "HT Chip MiSeq/HiSeq Analysis"
date: "October 1, 2015"
output: 
  html_document: 
    highlight: haddock
    theme: flatly
---


```{r plots, echo=FALSE}
    genesDetectedDensity_MiSeq <- ggplot(meta.miseq) + geom_density(aes(genesDetected, fill=column, color=seqRun), alpha=0.2) + scale_x_continuous(limits=c(0,2000), breaks=seq(0, 2000, 100)) + ggtitle("Genes Detected across cells from MiSeq Runs")
    return(genesDetectedDensity_MiSeq)

genesDetectedHistogram_MiSeq <- ggplot(meta.miseq) + geom_bar(aes(genesDetected, fill=column, color=seqRun), position="dodge", binwidth=50, alpha=0.2) + scale_x_continuous(limits=c(0,2000), breaks=seq(0, 2000, 100)) + ggtitle("Genes Detected across cells from MiSeq Runs")
return(genesDetectedHistogram_MiSeq)
```

This produces the following:

enter image description here

UPDATE: Following the suggestion I received below, I tried using the gridExtra library, and printed the plots by adding:

grid.arrange(genesDetectedDensity_MiSeq, genesDetectedHistogram_MiSeq, ncol=2)

This almost works, but it's still kind of messy:

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46

4 Answers4

20

You can use grid.arrange() in the gridExtra library to achieve this :)

Edit : using iris see the image :

library(gridExtra)

 plot1 <- qplot(iris$Sepal.Length)
 plot2 <- qplot(iris$Sepal.Width)

 grid.arrange(plot1, plot2, ncol=2)

link

tjebo
  • 21,977
  • 7
  • 58
  • 94
Bg1850
  • 3,032
  • 2
  • 16
  • 30
14

As long as your using a standard R Markdown html-report you can use the fact that R Markdown uses bootstrap to get two graphs side by side. If you have long titles you can use the escape character \n to line break them, as so:

---
title: "test"
author: "Testperson"
output:
  html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

<div class = "row">
<div class = "col-md-6">
```{r cars,  warning = FALSE, echo = FALSE, dev=c('svg')}
plot(pressure, main = paste("Lorem Ipsum ",
                        "Ipsum lorem ipsum. ",
                        "\nLorem ipsum", sep=""))
```
</div>
<div class = "col-md-6">
```{r pressure, warning = FALSE, echo=FALSE, dev=c('svg')}
plot(pressure, main = paste("Lorem Ipsum ",
                        "Ipsum lorem ipsum. ",
                        "\nLorem ipsum", sep=""))
```
</div>
</div>
ErrantBard
  • 1,421
  • 1
  • 21
  • 40
  • 2
    awesome +1 - I learned a lot, thanks. For the future reader, these are responsive columns, depending on the viewport size. The figures however in this setting become quite large and one needs to enlarge it pretty much in order to see the effect of two figures side by side. In order to always have the figures side by side, no responsive columns, one can also simply change width and display options for the div
    chunk
    – tjebo Apr 18 '20 at 17:21
6

You can also change the out.width to <=50% in the chunk options.

Need to have echo = FALSE in the chunk options!

---
title: "Side by Side"
output: html_document
---


You can also change the figure output options. 

```{r base plot, warning = FALSE, echo=FALSE, out.width="50%"}

plot(pressure, main = paste("Lorem Ipsum ",
                        "Ipsum lorem ipsum. ",
                        "\nLorem ipsum", sep=""))

plot(pressure, main = paste("Lorem Two ",
                        "Ipsum lorem ipsum. ",
                        "\nLorem ipsum", sep=""))
```

or, with ggplot objects

```{r ggplot2, warning = FALSE, echo=FALSE, out.width="50%"}
library(ggplot2)
ggplot(pressure, aes(temperature, pressure)) +
  geom_point() +
  ggtitle("plot1")

ggplot(pressure, aes(temperature, pressure)) +
  geom_point() +
  ggtitle("plot2")
```

result

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 1
    This is how I do it in base R but in ggplot2 it doesn't work and they go below each other. – adunaic Oct 25 '21 at 18:59
  • @adunaic sorry to hear it doesn't work for you. It works for me! (see my example screenshot in the answer). Might be your chunk settings. Good luck finding the problem! – tjebo Oct 26 '21 at 10:43
  • 2
    I ended up using the `patchwork` package as it was really easy (although your answer would be easier if it had worked out of the box). – adunaic Oct 27 '21 at 13:21
3

Another simple and straightforward option is using the patchwork package. From the docs:

library(patchwork)
library(ggplot2)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

# for side by side:
p1 + p2

# or to stack the plots:
p1 / p2
sbha
  • 9,802
  • 2
  • 74
  • 62
  • This is a really neat and easy to use package. Much easier than the grid packages for the simple stuff. – adunaic Oct 25 '21 at 19:02