2017-07-30 28 views
0

我的facet_wrap地块数量不平衡,这意味着我最终只有一个地块。我希望这个情节能够集中在X轴标题上,但一直没有弄清楚。感谢任何帮助。r facet_wrap不规则数量的地块,x轴中心底部地块标题

简单的例子来说明我的问题而努力着:

library(ggplot2) 
p <- ggplot(head(diamonds, 50), aes(x = depth, y = price)) + 
    geom_point() 

p <- p + facet_wrap(~color, scales = "free", ncol = 3) 
+0

我不认为你可以用小包装做到这一点。也许与面网格。 – Hatshepsut

回答

1

你可以在gtable洗牌周围的东西;不幸的是,名字出现有些不一致

g <- ggplotGrob(p) 

g$layout[grepl("panel-3-1", g$layout$name), c("l","r")] <- g$layout[grepl("panel-2-2", g$layout$name), c("l","r")] 
g$layout[grepl("axis-l-3-1", g$layout$name), c("l","r")] <- g$layout[grepl("axis-l-2-2", g$layout$name), c("l","r")] 
g$layout[grepl("axis-b-1-3", g$layout$name), c("l","r")] <- g$layout[grepl("axis-b-2-2", g$layout$name), c("l","r")] 
g$layout[grepl("strip-t-1-3", g$layout$name), c("l","r")] <- g$layout[grepl("strip-t-2-2", g$layout$name), c("l","r")] 

grid.newpage() 
grid.draw(g) 

enter image description here

+0

谢谢,这看起来像我可以合作的东西!在研究这个问题时,我遇到了grobs的概念,但我仍然不太了解它们。你对我在哪里可以学到更多关于在R中操作它们的建议吗? – Jaeoc

+0

这个特定的问题更多的是关于gtable布局,基本上没有文档。 [此页面可能有所帮助](https://github.com/baptiste/gridextra/wiki/gtable) – baptiste