2017-12-03 26 views
3

私は2x2プロットの配置をしています。プロットは同じ軸を共有するので、それらを一緒に配置したいと思います。plot_gridを使って余白無くプロットする方法は?

このコード:

library(ggplot2) 
library(cowplot) 

Value <- seq(0,1000, by = 1000/10) 
Index <- 0:10 
DF <- data.frame(Index, Value) 


plot <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme(aspect.ratio = 0.5) 

plot_grid(plot, plot, plot, plot, align = "hv", ncol = 2) 

enter image description here

を生成しかし、私は何か欲しい:

enter image description here

どのように私は、同様の結果を得ることができますか?

答えて

2

私は、これはeggパッケージからggarrange()関数の場合だと思います。 plot_grid()でこれを行うには、無限に手を加える必要があり、価値がありません。

(技術的な理由から、plot_grid()はグリッド定数の各プロットの合計面積を保持しますが、x軸を持つプロットとx軸を持たないプロットがある場合は、それぞれ異なる領域を占める場合があります。 rel_heights引数を使用しても、rel_heightsの正しい値を計算する良い方法はないので、試行錯誤します。ggarrange()は、プロットパネルと周囲の要素を別々に見て、プロットパネルのサイズが同じであることを確認します。ここ)

ggarrange()を使用してコードである:

Value <- seq(0,1000, by = 1000/10) 
Index <- 0:10 
DF <- data.frame(Index, Value) 


pbase <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme_bw() 

ptopleft <- pbase + 
    scale_x_continuous(position = "top") + 
    theme(plot.margin = margin(5.5, 0, 0, 5.5), 
     axis.title.x = element_blank(), 
     axis.text.x = element_blank(), 
     axis.ticks.x = element_blank()) 

ptopright <- pbase + 
    scale_y_continuous(position = "right") + 
    scale_x_continuous(position = "top") + 
    theme(plot.margin = margin(5.5, 5.5, 0, 0), 
     axis.title.x = element_blank(), 
     axis.text.x = element_blank(), 
     axis.ticks.x = element_blank()) 

pbottomleft <- pbase + 
    theme(plot.margin = margin(0, 0, 5.5, 5.5)) 

pbottomright <- pbase + 
    scale_y_continuous(position = "right") + 
    theme(plot.margin = margin(0, 5.5, 5.5, 0)) 

library(egg)  
ggarrange(ptopleft, ptopright, 
      pbottomleft, pbottomright, 
      ncol = 2) 

enter image description here

二つのコメント:

  1. トップのプロットにプロットパネル下のスペースのすべての最後のビットを削除するには、私たちがしているにもかかわらず、先頭にx軸を移動する必要がありますそれを示していない。これは、テーマメカニズムの奇妙な制限です。 1つの軸だけを完全に取り除くことはできません。

  2. 私はあなたの例のように、共有軸タイトルのファンではありません。私は各軸にタイトルがあるはずだと思う。共有軸のタイトルが必要な場合は、ファセット作成メカニズムを使用しないのはなぜですか?

2

微妙にplot.marginのプロットを設定し、次にgrid.arrangeを設定してラボを追加することができます。

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

Value <- seq(0,1000, by = 1000/10) 
Index <- 0:10 
DF <- data.frame(Index, Value) 

plot1 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme_minimal() + 
    theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA), 
    axis.text.x = element_blank(), 
    axis.title = element_blank(), 
    axis.ticks = element_blank(), 
    plot.margin = unit(c(5.5, 5.8, -50, 5.5), "pt")) 

plot2 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme_minimal() + 
    theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA), 
    axis.text.x = element_blank(), 
    axis.title = element_blank(), 
    axis.ticks = element_blank(), 
    plot.margin = unit(c(5.5, 5.5, -50, 5.5), "pt")) + 
    scale_y_continuous(position = "right") 

plot3 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme_minimal() + 
    theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA), 
    axis.title = element_blank(), 
    axis.ticks = element_blank(), 
    plot.margin = unit(c(-50, 5.8, -50, 5.5), "pt")) 

plot4 <- ggplot(DF, aes(x = Index, y = Value)) + 
    geom_line(linetype = 2) + 
    theme_minimal() + 
    theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA), 
    axis.title = element_blank(), 
    axis.ticks = element_blank(), 
    plot.margin = unit(c(-50, 5.5, -50, 5.5), "pt")) + 
    scale_y_continuous(position = "right") 

grid.arrange(grobs = list(plot1, plot2, plot3, plot4), ncol = 2, bottom = 'Index', left = 'Value', right = 'Value') 

最終プロット enter image description here

関連する問題