3

こんにちは、R と ggplot2 コミュニティです。

で作成された 2 つのプロットを水平方向に並べたいと思いggplot2ます。1 つにはファセット (およびファセット ストリップ!) と凡例の両方がありますが、2 つ目はありません。それらは同じ Y 軸を共有しており、これを揃えたいと考えています。グロブの幅を使用してプロットを垂直方向に配置しても問題ありませんが、高さではわかりませんでした。

ここに私の質問をサポートするコードがあります。

library( ggplot2 )
library( gridExtra )

plot_1 <- ggplot() +
  geom_point(
    data = iris,
    aes(
      x = Petal.Length,
      y = Petal.Width,
      colour = Sepal.Length 
      ) 
    ) +
  facet_grid(
    . ~ Species 
    ) +
  theme(
    legend.position = "bottom"
    )

plot_2 <- ggplot() +
  geom_point(
    data = iris,
    aes(
      x = Sepal.Width,
      y = Petal.Width
      )
    )

g1 <- ggplotGrob( plot_1 )
g2 <- ggplotGrob( plot_2 )

# Here, how to manipulate grobs to get plot_2 scaled to the plot_1's Y axis? (ie, with an empty area right of the plot_1's legend)

grid.arrange( g1, g2, ncol = 2 )

以前にグロブの高さを操作する方法を知っていますgrid.arrange()か? (他の提案は大歓迎です!) さらに、たとえば総面積の 3 分の 2 を に割り当てる方法はplot_1?

私の実際のプロットには実際には が含まれていることにも注意してください。ただしcoord_flip()、この質問には関係ないと思います。

ご協力いただきありがとうございます!

4

1 に答える 1

1

簡単な解決策は、空白のパネルを作成することです。

blank<-grid.rect(gp=gpar(col="white"))

次に、grid.arrange を使用して、2 番目の列が実際には 3 行で構成される 2 列のグリッドを作成します。

grid.arrange(g1, arrangeGrob(blank,g2,blank,ncol=1,
heights=c(0.2,0.9,0.2)),ncol=2) 

引数をいじっheightsてみると、望ましい結果が得られるはずです。

あなたの追加の質問に関しては、heightswidthsの引数もgrid.arrange非常に役立つはずです。

于 2015-03-23T14:58:31.313 に答える