r facet_wrap图表的数量不均匀,中心底部图表超过x轴标题

时间:2017-07-30 08:34:52

标签: r ggplot2 facet

我的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)

1 个答案:

答案 0 :(得分: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