我想手动(或自动)更改 R 中 ggplot2 图形的分面图的面板布局。我已经看到了注释和分面重新排序的解决方案,但没有看到这个具体问题。这是一个可重现的示例:
library(ggplot2)
plot <- ggplot(diamonds, aes(carat, price)) + facet_wrap(~cut) + geom_point()
如果我现在查看该图,您会发现空白面分配在绘图网格的右下角。
All I want to do is make the blank plot location be in the top left corner instead, but still plot all the other plots (just move the blank plot location).
我试过看ggplot_build()
像这样:
plot_build <- ggplot_build(plot)
plot_build$panel$layout
但我不知道如何实际将空白绘图位置移动到正确的行和列。有人有什么想法吗?
您可以在 gtable 中移动面板,
library(grid)
library(ggplot2)
p <- ggplot(diamonds, aes(carat, price)) +
facet_wrap(~cut) + geom_point()
g <- ggplotGrob(p)
gl <- g$layout
idcol <- gl$r == (ncol(g)-2)
g$layout[idcol & gl$b < 5, c("t", "b")] <- gl[idcol & gl$b < 5, c("t", "b")] + 4
grid.newpage()
grid.draw(g)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)