在R markdown HTML文件中并排打印ggplot数字?

时间:2015-10-16 23:47:43

标签: r ggplot2 rstudio r-markdown

我将带有ggplot的两个绘图打印到R Markdown HTML输出中,但我希望它们能够并排出现。这可能吗? 我也可以设定数字的大小吗?

到目前为止,我只能一个接一个地打印它们。我也尝试过R Cookbook中的多重绘图功能,但这严重扭曲了这些情节......

谢谢!

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

这会产生以下结果:

enter image description here

更新:根据我在下面收到的建议,我尝试使用gridExtra库,并通过添加以下内容打印图表:

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

这几乎可行,但它仍然有点混乱:

enter image description here

4 个答案:

答案 0 :(得分:15)

您可以在gridExtra库中使用grid.arrange()来实现此目的:)

修改:使用光圈查看图片:link

 library(gridExtra)

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

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

答案 1 :(得分:6)

只要您使用标准的R Markdown html报告,您就可以使用R Markdown使用引导程序并排获得两个图表的事实。如果您有长标题,则可以使用转义字符\n对其进行换行,如下所示:

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

答案 2 :(得分:2)

您还可以在块选项中将out.width更改为<= 50%。

需要在块选项中使用echo = FALSE

---
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")
```

结果

enter image description here

答案 3 :(得分:0)

另一个简单直接的选择是使用 patchwork 包。来自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