「facet_grid プロットのすべての面に対して X 軸のテキストを強制的にオンにする」という投稿で、私の質問に対する解決策の大部分を見つけることができて、とてもうれしく思いました。
2 行以上のファセットがあることを除いて、OP の Drew Steen のものに似たグラフを作成したいと思います。また、行ごとに異なる x 軸ラベルを作成したいと考えています。
私は @baptiste の素晴らしい回答から超ハックなソリューションを作成しました (ほとんどの場合、私は gtable パッケージに慣れていないため)、知りたいです:
- 以下に書いた混乱よりもエレガントな解決策があれば
- 「プレミアム」(中)行のラベルを挿入する方法。
これは、@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)