2012-04-07 64 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