获取情节下方的刻面条很容易,
library(gtable)
g <- ggplotGrob(p)
strips <- gtable_filter(g, "strip_t", trim=FALSE)
grid.newpage()
grid.draw(rbind(g, strips[3,], size="first"))
然而,轴需要更加小心,因为必须颠倒刻度线和标签的位置。你也许可以从这个开始,
tweak_axis <- function(a){
inner <- a[["children"]]["axis"][[1]]
inner[["grobs"]] <- rev(inner[["grobs"]])
inner$grobs[[2]]$y <- inner$grobs[[2]]$y - unit(0.15, "cm")
a[["children"]]["axis"][[1]] <- inner
a
}
axes <- gtable_filter(g, "axis_b", trim=FALSE)
axes$grobs <- lapply(axes$grobs, tweak_axis)
grid.newpage()
grid.draw(axes)
编辑:基于上述,一个“完整”的解决方案可能是
grid.newpage()
g2 <- g
new_axes <- lapply(g2$grobs[grepl("axis_b", g2$layout$name)], tweak_axis)
g$grobs[grepl("strip_t", g$layout$name)] <- new_axes
g$grobs[grepl("axis_b", g$layout$name)] <- g2$grobs[grepl("strip_t", g2$layout$name)]
# heights should be changed too, but it's kind of ok here
xlab <- 7; title <- 1:2
grid.draw(rbind(g[xlab,], g[-c(title, xlab), ], size="last"))
(有明显的警告)