Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing figures side by side in R markdown HTML file?

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

like image 346
gaelgarcia Avatar asked Oct 16 '15 23:10

gaelgarcia


People also ask

What is the difference between R Markdown and markdown?

R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.

How do I show plots in R Markdown?

In RStudio, when you open a new RMarkdown file, in the editor pane, there is a cogwheel button / menu where you can choose "Chunk Output Inline". That should put the plots into the document.


1 Answers

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

like image 70
Bg1850 Avatar answered Oct 26 '22 19:10

Bg1850