4

Rマークダウンを使用してpdfドキュメントを生成し、 forループでggplotを使用して作成された一連のプロットを表示したいと思います。たとえば、1 ページあたり 2 X 2 グリッドのプロットがあるように、各ページのプロット数を制御することは可能ですか?

適切なページ数に分割されるように、グラフの総数を変更できるように柔軟性を維持したいと思います。

ggplot2 および gridExtra パッケージを使用した私の試みは以下のとおりですが、ページごとのグラフの数を制御することはできません。それらを 1 つのページのみに絞り込みたいようです。grid.arrange の ncol,nrow,heights,widths 引数を変更しようとしましたが、役に立たないようです。

    ---
    title: "ggplot Layout"
output: pdf_document
    ---

    ```{r,figure2 fig.height=8, fig.width=6,echo=FALSE,message=FALSE}


    library(ggplot2)
    library(gridExtra) 

    graphlist<-list()
    count <- 1
    colnums<-c(1,5,6,7,8,9,10)

    for (i in colnums) {
    plot.x.name<-names(diamonds[i])
    p <- ggplot(data=diamonds,aes_string(x = plot.x.name)) + geom_histogram() 
    graphlist[[count]]<-p
    count <- count+1

    }

    do.call("grid.arrange",c(graphlist,ncol=2))



    ```

私が探しているもののタイプは、このコード ( http://kbroman.github.io/knitr_knutshell/pages/figs_tables.htmlから適応) によって示されていますが、残念ながら、これは ggplot では機能しません。

---
title: "Example Layout"
output: pdf_document
---


```{r bunch_o_figs,fig.height=8, fig.width=6, message=FALSE,echo=FALSE}
n <- 100
x <- rnorm(n)
par(mfrow=c(2,2), las=1)
for(i in 1:20) {
  y <- i*x + rnorm(n)
  plot(x, y, main=i)
}
``
4

1 に答える 1

6

あなたは試すことができますmarrangeGrob

---
title: "ggplot Layout"
output: pdf_document
---

```{r figure2 , fig.height=8, fig.width=6,echo=FALSE,message=FALSE}


    library(ggplot2)
    library(gridExtra) 

    graphlist <- replicate(10, qplot(1,1), simplify = FALSE)

    do.call("marrangeGrob",c(graphlist,ncol=2,nrow=2))
```
于 2014-07-08T18:44:40.630 に答える