web-dev-qa-db-ja.com

ggplotの「アンバランス」グリッドを取得するにはどうすればよいですか?

grid.arrangeを使用すると、複数のggplot図形をグリッドに配置して、次のようなものを使用してマルチパネル図形を実現できます。

library(ggplot2)
library(grid)
library(gridExtra)

いくつかのggplot2プロットを生成してから、

plot5 <- grid.arrange(plot4, plot1, heights=c(3/4, 1/4), ncol=1, nrow=2)

最初の列全体に1つのプロットがあり、2番目の列に3つのプロットがある「アンバランス」な2列のレイアウトを取得するにはどうすればよいですか?私はgrid.arrangeを使用して別のプロットに対して1つのグリッド(たとえば、上記のplot5)をプロットしようとする「グリッドのグリッド」アプローチをいじりましたが、

ArrangeGrob(...、as.table = as.table、clip = clip、ma​​in = main、でのエラー:入力はグロブでなければなりません!

更新:

アドバイスをありがとう。 viewportsgridを調べます。それまでの間、@ DWinのおかげで、 'wq'パッケージのlayOut関数は、私のSweaveドキュメントのコンパイル図で非常にうまく機能しました。 enter image description here

更新2:

arrangeGrobcommand(@baptisteによって示唆された)もうまく機能し、非常に直感的に見えます-少なくとも2つの列の幅を変更するのは簡単でした。また、 `wq 'パッケージを必要としないという利点もあります。

例えばSweaveファイルのコードは次のとおりです。

<<label=fig5plot, echo=F, results=hide>>=
plot5<-grid.arrange(plot4, arrangeGrob(plot1, plot2, plot3, ncol=1), 
                    ncol=2, widths=c(1,1.2))
@
\begin{figure}[]
    \begin{center}
<<label=fig5,fig=TRUE,echo=T, width=10,height=12>>=
<<fig5plot>>
@
\end{center}
\caption{Combined plots using the `arrangeGrob' command.}
\label{fig:five}
\end{figure}

次の出力が生成されます。 enter image description here

ところで、誰が '> NA'が表示されるのか教えてください。

93
user441706

grid.arrangeデバイスに直接描画します。他のグリッドオブジェクトと組み合わせる場合は、次のようにarrangeGrobが必要です。

 p = rectGrob()
 grid.arrange(p, arrangeGrob(p,p,p, heights=c(3/4, 1/4, 1/4), ncol=1),
              ncol=2)

編集(2015年7月):v> 2.0.0では、layout_matrix引数、

 grid.arrange(p,p,p,p, layout_matrix = cbind(c(1,1,1), c(2,3,4)))
70
baptiste

私はグリッドでそれを理解しようとしましたが、ダウンしたと思ったが失敗しました(以下で引用する関数のコードを見ると、私は本当に近かったことがわかります... :-)

'wq'パッケージには、それを行うlayOut関数があります。

p1 <- qplot(mpg, wt, data=mtcars)
layOut(list(p1, 1:3, 1),   # takes three rows and the first column
        list(p1, 1, 2),    # next three are on separate rows
         list(p1, 2,2), 
          list(p1, 3,2))

enter image description here

17
42-

別の選択肢は、Thomas Lin Pedersenによる patchwork パッケージです。

# install.packages("devtools")
# devtools::install_github("thomasp85/patchwork")
library(patchwork)

いくつかのプロットを生成します。

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + facet_grid(rows = vars(gear))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
p4 <- ggplot(mtcars) + geom_bar(aes(carb))

次に、プロットを配置します。

p1 + (p2 / p3 / p4)

enter image description here

2
markus

言及する価値がある multipanelfigure package もあります。こちらもご覧ください answer

library(ggplot2)
theme_set(theme_bw())

q1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
q2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
q3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
q4 <- ggplot(mtcars) + geom_bar(aes(carb))

library(magrittr)
library(multipanelfigure)

figure1 <- multi_panel_figure(columns = 2, rows = 3, panel_label_type = "upper-roman")

figure1 %<>%
  fill_panel(q1, column = 1, row = 1:3) %<>%
  fill_panel(q2, column = 2, row = 1) %<>%
  fill_panel(q3, column = 2, row = 2) %<>%
  fill_panel(q4, column = 2, row = 3)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
figure1

reprexパッケージ (v0.2.0.9000)によって2018-07-16に作成されました。

1
Tung