在ggplot2中将一个面板留空

时间:2013-12-12 16:48:21

标签: r ggplot2

p <- ggplot(mtcars, aes(mpg, wt)) 
p + geom_point()+facet_grid(cyl ~ vs)+theme_bw()

Pic

我想将面板(右下角的1对8)留空而没有显示数据点,但同时,我想保留这种安排。

所以facet_wrap(cyl ~ vs)无法解决我的问题。

也许更一般的问题是,是否可以在ggplot2中安排每个面板?

2 个答案:

答案 0 :(得分:12)

你可以这样做,但不能用facet_wrap(据我所知)。创建单独的子图。有关详细的分步方法,see my answer here

创建一个空白的情节&amp;使用包gridExtra,您可以组合图:

library(gridExtra)
library(grid)

blank <- grid.rect(gp=gpar(col="white"))

grid.arrange(plot1, plot2, blank, plot3, ncol=2)

这种方法也会对你最终情节(恕我直言)的外观产生很大的影响。

答案 1 :(得分:11)

您更改了表格。

## get the table grobs
g1 <- ggplot_gtable(ggplot_build(p))

library(gtable)
library(grid)
## here the main modification
## change one panel by a new rectangle.
pp <- gtable_add_grob(g1,rectGrob(gp=gpar(col=NA)),t=8,l=6,b=8,r=6)
grid.draw(pp)

enter image description here