4

facet_grid プロットのすべての面に対して X 軸のテキストを強制的にオンにするという投稿で、私の質問に対する解決策の大部分を見つけることができて、とてもうれしく思いました。

2 行以上のファセットがあることを除いて、OP の Drew Steen のものに似たグラフを作成したいと思います。また、行ごとに異なる x 軸ラベルを作成したいと考えています。

私は @baptiste の素晴らしい回答から超ハックなソリューションを作成しました (ほとんどの場合、私は gtable パッケージに慣れていないため)、知りたいです:

  1. 以下に書いた混乱よりもエレガントな解決策があれば
  2. 「プレミアム」(中)行のラベルを挿入する方法。

これは、@Drew Steen と @baptiste から採用したコードです。

library(ggplot2)

diamondSub <-subset(diamonds, (cut=="Ideal" | cut=="Premium" | cut == "Very Good") & (color=="E" | color=="I"))

p<- ggplot(diamondSub, aes(x=carat, y=price)) + 
  geom_blank()+ 
  geom_point() +
  scale_x_discrete(breaks=c(1, 2, 3, 4), labels=c("a", "b", "c", "d")) +
  facet_grid(cut~color, scales="free_x")
p

p2<- ggplot(diamondSub, aes(x=carat, y=price)) + 
  geom_blank()+ 
  geom_point() +
  scale_x_discrete(breaks=c(1, 2, 3, 4), labels=c("f", "g", "h", "i")) +
  facet_grid(cut~color, scales="free_x")
p2

library(gtable)

g <- ggplotGrob(p)
g2 <- ggplotGrob(p2)

# locate the panels
panels <- grep("panel", g$layout$name)
panels2 <- grep("panel", g2$layout$name)

top <- unique(g$layout$t[panels])
top2 <- unique(g2$layout$t[panels2])
# intersperse a copy of the bottom axes
all <- gtable:::rbind_gtable(gtable:::rbind_gtable(g[seq.int(min(top)), ], 
                                                   g[max(top)+1,], "first"), 
                             g2[seq(min(top2)+1, nrow(g2)),], "first")
grid.newpage()
grid.draw(all)

グラフを見る

4

1 に答える 1

3

代替バージョン (2015 年 4 月 24 日に追加) は、要素の手動構築を示し、より多くのコメントを含んでいます。

## Alternative version
library(gtable)
library(grid)

# Get the ggplot grobs
g <- ggplotGrob(p)
g2 <- ggplotGrob(p2)

# Show the layout.
# Note the rows and columns
# In this case, the g2 layout is the same as the g layout
gtable_show_layout(g)

# The large panels are the plot panels.
# We will need rows 4, 6 and 8.

# For the top "Very Good" row, we will also need rows 1, 2, and 3.

# The axis is located in row 9

# Therefore rbind the grob in rows 1, 2, 3, and 4, with the grob in row 9.
top.row <- rbind(g[1:4, ], g[9, ], size = "first")

# The second "Premium" row
# We need the panels in row 6 plus the small gap row above,
# and rbind that to the axis
middle.row = rbind(g2[5:6, ], g2[9,], size = "first")

# The bottom "Ideal" row
# We need the panel in row 8 plus the small gap in the row above
# plus the axis in the row below
# plus rows below that (axis label and margin)
bottom.row = g2[7:11, ]


# rbind the three rows 
all <- rbind(rbind(top.row, middle.row, size = "first"), bottom.row, size = "first")

# Draw it
grid.newpage()
grid.draw(all)

# Maybe add a little more space between the rows
gtable_show_layout(all)
all$heights[c(6,9)] = unit(1, "lines")

# Draw it
grid.newpage()
grid.draw(all)

しかし、パネルを軸にバインドする必要さえありません。レイアウトから適切な行を選択するだけです:

top.row <- g[c(1:4, 9), ]
middle.row = g2[c(5:6, 9), ]
bottom.row = g2[7:11, ]

次に、これら 3 つの新しい行をバインドします。

元のバージョン

間違いを犯したのは行の結合だけです。軸を一番上の「非常に良い」行にバインドしました。一番下の「理想」行にはすでに軸があるため、バインディングは必要ありません。修正方法: 中央の「プレミアム」行には軸が必要です。

各行を個別に作成し、軸を一番上の行と真ん中の行にバインドしてから、3 つの行をバインドしました。

行間にもう少しスペースを追加するための別のステップを追加しました。

g <- ggplotGrob(p)
g2 <- ggplotGrob(p2)

# locate the panels
panels <- grep("panel", g$layout$name)
panels2 <- grep("panel", g2$layout$name)

top <- unique(g$layout$t[panels])
top2 <- unique(g2$layout$t[panels2])

# Construct each row separately
top.row <- gtable:::rbind_gtable(g[seq.int(min(top)), ], g[max(top)+1,], "first")
middle.row <- gtable:::rbind_gtable(g2[c(top[2]-1,top[2]), ], g2[max(top)+1,], "first")
bottom.row <- g2[(max(top2)-1):nrow(g2), ]

all <- gtable:::rbind_gtable(gtable:::rbind_gtable(top.row, middle.row, "first"), bottom.row, "first")

# Draw it
grid.newpage()
grid.draw(all)

# Maybe add a little more space between the rows
all$heights[c(6,9)] = unit(1, "lines")

grid.newpage()
grid.draw(all)

ここに画像の説明を入力

于 2015-04-22T02:17:16.260 に答える