2012-04-07 63 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) – 2012-04-27 08:20:01

+0

謝謝!我沒有意識到這是如此困難 – 2012-05-02 01:38:12

+0

另請參見:http://stackoverflow.com/questions/3261597/can-i-change-the-position-of-the-strip-label-in-ggplot-from-the- top-to-the-botto(另一個否定答案) – 2012-05-09 19:34:07

回答

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

如果你和我一樣,而且你想要在*軸上面的條形標籤*,你可以使用'gt = gtable_add_grob(gt,stripGrob,t = max $ b)+ 1,l = min(面板$ l),r = max(面板$ r))'代替桑迪的 – Nova 2016-04-06 13:57:19