drm*_*iod 8 r ggplot2 gridextra
我创建了一个图形,ggplot后来我用它arrangeGrob来组合这些图形.有没有办法从这个组合图中删除图形的部分?或者可能提取?
这是一个最小的例子:
library(ggplot2)
library(gridExtra)
df <- data.frame(x=rnorm(20), y=rnorm(20), y2=rnorm(20))
g1 <- ggplot(df, aes(x, y)) + geom_point()
g2 <- ggplot(df, aes(x, y2)) + geom_point()
g <- arrangeGrob(g1,g2, widths=c(3.5,7.5), ncol=2)
print(g)
Run Code Online (Sandbox Code Playgroud)
我想删除两个图中的一个.
首先,使用grid.ls()查看构成情节的怪物列表。在这里,您将查找对gTree各个图进行编码的两个对象的名称。(与lattice相比,ggplot2对组件组的命名相对没有帮助,尽管在这种情况下,不难看出您想要提取哪些部分。)
grid.ls()
# GRID.arrange.90
# GRID.frame.84
# GRID.cellGrob.85
# GRID.frame.5
# GRID.cellGrob.44
# GRID.gTree.42
# GRID.clip.43
# layout
# GRID.cellGrob.83
# GRID.gTree.81
# GRID.clip.82
# layout
# GRID.cellGrob.86
# GRID.null.1
# GRID.cellGrob.87
# GRID.null.2
# GRID.cellGrob.88
# GRID.null.4
# GRID.cellGrob.89
# GRID.null.3
Run Code Online (Sandbox Code Playgroud)
然后,您可以像这样提取并绘制它们:
gg1 <- getGrob(g, ("GRID.gTree.42"))
grid.draw(gg1)
gg2 <- getGrob(g, ("GRID.gTree.81"))
grid.draw(gg2)
Run Code Online (Sandbox Code Playgroud)