6

ggplot2 2.0.0 にアップデートしてから、gridExtra を使ってチャートをうまく配置できません。問題は、ファセット チャートが圧縮され、他のチャートが拡大することです。幅は基本的にめちゃくちゃです。これらの単一ファセット プロットと同様に配置したい: 2 つのグラフ エッジを左揃え (ggplot)

再現可能なコードを入れます

library(grid) # for unit.pmax()
library(gridExtra)

plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  facet_grid(. ~ Species) + 
  stat_smooth(method = "lm")

plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5)

g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable

iris.widths <- g.iris$widths # extract the first three widths, 
mpg.widths <- g.mpg$widths # same for mpg plot
max.widths <- unit.pmax(iris.widths, mpg.widths)

g.iris$widths <- max.widths # assign max. widths to iris gtable
g.mpg$widths <- max.widths # assign max widths to mpg gtable

grid.arrange(g.iris,g.mpg,ncol=1)

ここに画像の説明を入力

ご覧のとおり、上のグラフでは、最初のファセットが拡大され、他の 2 つは右側で圧縮されています。下のグラフはすべての幅をカバーしていません。

新しい ggplot2 バージョンが gtable の幅を台無しにしている可能性はありますか?

誰でも回避策を知っていますか?

どうもありがとうございました

編集:チャートの写真を追加

私は次のようなものを探しています:

ここに画像の説明を入力

4

4 に答える 4

5

1 つのオプションは、中央セルがすべてのプロット パネルをラップする 3x3 gtable に各プロットをマッサージすることです。

@SandyMuspratt の例を使用する

# devtools::install_github("baptiste/egg") 
grid.draw(egg::ggarrange(plots=plots, ncol=1))

ここに画像の説明を入力

この標準化された形式では、パネル、凡例、軸、ストリップなどの数に関係なく、プロットをさまざまなレイアウトで簡単に組み合わせることができるという利点があります。

grid.newpage()
grid.draw(ggarrange(plots=list(p1,  p4, p2, p3), widths = c(2,1), debug=TRUE))

ここに画像の説明を入力

于 2016-04-04T10:47:30.930 に答える
1

まだ解決策を探しているかどうかはわかりませんが、これはかなり一般的です。私はggplot 2.1.0を使用しています(現在はCRAN上にあります)。このソリューションに基づいています。問題を 2 つの部分に分けます。まず、プロットの左側を処理し、軸のマテリアルの幅が同じであることを確認します。これはすでに他の人によって行われており、SO に関する解決策があります。しかし、結果は良くないと思います。パネルも右側に配置することをお勧めします。2 番目の手順では、パネルの右側の列の幅が同じであることを確認します。これは、各プロットの右側に適切な幅の列を追加することによって行われます。(もっときちんとした方法があるかもしれません。@baptiste ソリューションを参照してください。)

library(grid)    # for pmax
library(gridExtra) # to arrange the plots
library(ggplot2)   # to construct the plots
library(gtable)   # to add columns to gtables of plots without legends

mpg$g = "Strip text"  

# Four fairly irregular plots: legends, faceting, strips
p1 <- ggplot(mpg, aes(displ, 1000*cty)) + 
  geom_point() + 
  facet_grid(. ~ drv) + 
  stat_smooth(method = "lm")

p2 <- ggplot(mpg, aes(x = hwy, y = cyl, colour = factor(cyl))) + 
  geom_point() + 
  theme(legend.position=c(.8,.6),
        legend.key.size = unit(.3, "cm"))

p3 <- ggplot(mpg, aes(displ, cty, colour = factor(drv))) + 
  geom_point() + 
  facet_grid(. ~ drv) 


p4 <- ggplot(mpg, aes(displ, cty, colour = factor(drv))) + 
  geom_point() + 
  facet_grid(g ~ .) 

# Sometimes easier to work with lists, and it generalises nicely
plots = list(p1, p2, p3, p4)

# Convert to gtables
g = lapply(plots, ggplotGrob) 

# Apply the un-exported unit.list function for grid package to each plot
g.widths = lapply(g, function(x) grid:::unit.list(x$widths)) 


## Part 1: Make sure the widths of left axis materials are the same across the plots
# Get first three widths from each plot
g3.widths <- lapply(g.widths, function(x) x[1:3])

# Get maximum widths for first three widths across the plots
g3max.widths <- do.call(unit.pmax, g3.widths)

# Apply the maximum widths to each plot
for(i in 1:length(plots)) g[[i]]$widths[1:3] = g3max.widths

# Draw it
do.call(grid.arrange, c(g, ncol = 1))


## Part 2: Get the right side of the panels aligned
# Locate the panels
panels <- lapply(g, function(x) x$layout[grepl("panel", x$layout$name), ])

# Get the position of right most panel
r.panel  = lapply(panels, function(x) max(x$r)) # position of right most panel

# Get the number of columns to the right of the panels
n.cols = lapply(g.widths, function(x) length(x)) # right most column

# Get the widths of these columns to the right of the panels
r.widths  <- mapply(function(x,y,z) x[(y+1):z], g.widths, r.panel, n.cols)

# Get the sum of these widths
sum.r.widths <- lapply(r.widths, sum)

# Get the maximum of these widths
r.width = do.call(unit.pmax, sum.r.widths)

# Add a column to the right of each gtable of width 
# equal to the difference between the maximum
# and the width of each gtable's columns to the right of the panel. 
for(i in 1:length(plots)) g[[i]] = gtable_add_cols(g[[i]], r.width - sum.r.widths[[i]], -1)

# Draw it
do.call(grid.arrange, c(g, ncol = 1))

ここに画像の説明を入力

于 2016-03-07T06:00:40.753 に答える
0

この 2 行を削除して残りを維持すると、問題なく動作しました。

g.iris$widths <- max.widths # assign max. widths to iris gtable
g.mpg$widths <- max.widths # assign max widths to mpg gtable

ここに画像の説明を入力

おそらく、それらの幅を制限していたのでしょう。

于 2016-01-14T19:13:28.863 に答える