ggplot2移动构面布局 [英] ggplot2 move facet layout

查看:95
本文介绍了ggplot2移动构面布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想手动(或自动)更改R中ggplot2图形的多面图的面板布局.我已经看到了Facets注释和重新排序的解决方案,但没有这个特定问题.这是一个可重现的示例:

I would like to manually (or automatically) alter the panel layout of a faceted graph of a ggplot2 graphic in R. I have seen solutions to annotations and reordering of Facets, but not this specific question. Here is a reproducible example:

library(ggplot2)
plot <- ggplot(diamonds, aes(carat, price)) + facet_wrap(~cut) + geom_point()

如果我现在查看图,您会看到空白面分配在图网格的右下角.

If I look at the plot now, you see that the blank facet is allocated in the bottom right corner of the plot grid.

我要做的就是改为将空白图位置放在左上角,但仍然绘制所有其他图(只需移动空白图位置).

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

但是我不知道如何将空白打印位置实际移动到正确的行和列.有人有什么想法吗?

but I can't figure out how to actually move the blank plot location to the correct row and column. Does anyone have any ideas?

推荐答案

您可以在gtable中移动面板,

You could move panels in the 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)

这篇关于ggplot2移动构面布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆