如何刻面时,图下方显示标签条?(How to display strip labels below

2019-06-23 10:44发布

看来,带总是通过创建GGPLOT2情节之上。 他们可以移动的情节之下?

例如:

library(ggplot2) 
qplot(hwy, cty, data = mpg) + facet_grid( . ~ manufacturer)

显示在上面的汽车信息。 他们能在底部显示的是什么?

Answer 1:

更新:使用ggplot2版本2.1.0,可以考虑使用switch = 'x'?facet_grid了解详情。

使用gtable功能,很容易移动的带材。 (或参阅此处为花药版本-交换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)



文章来源: How to display strip labels below the plot when faceting?
标签: r ggplot2