2012-04-07 15 views
10

ストリップは常にggplot2によって作成されたプロットの上にあるようです。彼らはプロットの下に移動することはできますか?例えばファセット作成時にプロットの下にストリップラベルを表示する方法は?

library(ggplot2) 
qplot(hwy, cty, data = mpg) + facet_grid(. ~ manufacturer) 
トップの車の情報を表示し

。下に表示することはできますか?

+2

同様の質問がいくつかの時間の年齢ggplotメーリングリストに頼まれました。 [ここをクリック](http://groups.google.com/group/ggplot2/browse_thread/thread/415c00e3373c9cc8/8fb65cc4aa0849cf?lnk=gst&q=facet+text+label#8fb65cc4aa0849cf) –

+0

ありがとうございました!私はそれがとても難しいと気づいていませんでした –

+0

も参照してください:http://stackoverflow.com/questions/3261597/can-i-change-the-position-of-the-strip-label-in-ggplot-from-the- –

答えて

6

更新:ggplot2バージョン2.1.0を使用すると、switch = 'x'を使用することを検討してください。詳細は?facet_gridを参照してください。

gtableの機能を使用すると、ストリップを移動するのが簡単です。 (または、葯バージョンのhereを参照してください - x軸とストリップを交換する)

library(ggplot2) 
library(gtable) 
library(grid) 

p <- ggplot(mpg, aes(hwy, cty)) + geom_point() + facet_grid(. ~ manufacturer) + 
    theme(strip.text.x = element_text(angle = 90, vjust = 1), 
      strip.background = element_rect(fill = NA)) 

# Convert the plot to a grob 
gt <- ggplotGrob(p) 

# Get the positions of the panels in the layout: t = top, l = left, ... 
panels <-c(subset(gt$layout, grepl("panel", gt$layout$name), select = t:r)) 

# Add a row below the x-axis tick mark labels, 
# the same height as the strip 
gt = gtable_add_rows(gt, gt$height[min(panels$t)-1], max(panels$b) + 2) 

# Get the strip grob 
stripGrob = gtable_filter(gt, "strip-t") 

# Insert the strip grob into the new row 
gt = gtable_add_grob(gt, stripGrob, t = max(panels$b) + 3, l = min(panels$l), r = max(panels$r)) 

# remove the old strip 
gt = gt[-(min(panels$t)-1), ] 

grid.newpage() 
grid.draw(gt) 

enter image description here

+1

あなたが私のようで、ストリップラベル*をx軸より上にしたい場合は、gt = gtable_add_grob(gt、stripGrob、t = max(panels $ B)+ 1、l = min(パネル$ l)、r = max(パネル$ r)) ' – Nova

関連する問題