r facet_wrap 奇数图,x 轴标题上的中心底部图

r facet_wrap uneven number of plots, center bottom plot over x axis title

我的 facet_wrap 中的地块数量不均匀,这意味着我最后一行只有一个地块。我希望这个情节以 x-axis 标题为中心,但一直无法弄清楚。感谢您的帮助。

用于说明我的问题并使用的简单示例:

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

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

您可以在 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)