如何使用facet_wrap产生的空白空间? [英] How to use empty space produced by facet_wrap?
问题描述
我有一个形成 n x m
网格的多面图.按照设计,最后一个(右下角)单元格总是空的,所以我想通过添加另一个 ggplot 对象来利用额外的空间.我当前的解决方案依赖于低级 viewport
方法,这不是很优雅,并且需要对位置和大小进行一些硬编码.
I have a faceted plot that forms an n x m
grid. By design, the last (bottom-right) cell is always empty, so I'd like to utilize the extra space by adding another ggplot object. My current solution relies on low-level viewport
approach, which is not very elegant and requires some hard-coding for position and size.
相反,我认为可以通过其他方式访问空白空间,可能使用 gridExtra
?
Instead, I assume the empty space is reachable in some other fashion, probably with gridExtra
?
这是 n=m=2
的一个最小示例.请注意,边缘没有正确对齐,因此需要一些额外的工作来手动调整视口的参数,这很痛苦,尤其是如果 (n, m)
之后发生变化.
Here's a minimal example for n=m=2
. Note that the edges are not aligned properly, so some extra work is required to manually adjust viewport's parameters, which is a pain, especially if (n, m)
change afterwards.
library(ggplot2)
library(grid)
p <- qplot(displ, hwy, data = mpg[mpg$cyl != 5, ]) +
facet_wrap(~ cyl, nrow=2)
q <- qplot(date, unemploy, data = economics, geom = "line") +
labs(x = NULL, y = NULL)
p
print(q, vp=viewport(0.75, 0.275, 0.45, 0.45))
推荐答案
你可以像这样使用gtable
来访问空单元格"
You can use gtable
to access the "empty cell" like so
library(gtable)
pg <- ggplotGrob(p)
qg <- ggplotGrob(q)
pl <- gtable_filter(pg, 'panel', trim=F)$layout
pg <- gtable_add_grob(pg, qg, t=max(pl$t), l=max(pl$l))
grid.newpage()
grid.draw(pg)
n x m
方面的通用放置
这篇关于如何使用facet_wrap产生的空白空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!