【发布时间】:2015-10-23 14:40:16
【问题描述】:
我创建了一个图表,我通过这个命令在其中插入了另一个图表(两个 ggplot2 对象):
vp=viewPort(...)
print(ggplotobject1)
print(ggplotobject2, vp=vp)
这完全符合我的要求(一个大图,在 viewPort 中指定的区域中绘制了一个自定义小图)。
问题是我需要稍后使用这个组合图再次通过以下方式将其与其他图排列:
grid.arrange(arrangeGrob(..))
有人知道如何将我的组合图存储为 grob 吗?
真的非常感谢!
编辑: 在这里回应 baptiste 是一个可重现的例子:
library(ggplot2)
library(gtable)
library(grid)
data<-mtcars
main_plot<-ggplot(data,aes(hp,mpg,group=cyl))+
geom_smooth(method="lm")+geom_point()+
facet_grid(.~gear)
sub_plot<-ggplot(data,aes(disp,wt,color))+geom_point()
gtable_main<-ggplot_gtable(ggplot_build(main_plot))
gtable_sub<-ggplot_gtable(ggplot_build(sub_plot))
gtable_show_layout(gtable_main)
gtable_main2<-gtable_add_grob(gtable_main,gtable_sub,t=4,l=4,b=1,r=1)
grid.draw(gtable_main2)
这会生成我想要的图形,但我无法使子图的大小正确(它应该是图左下角的一个小图)。这可能是非常基本的,但我之前没有使用过gtable,并且只使用过一点点grid/gridExtra。
非常感谢!
【问题讨论】:
标签: r ggplot2 viewport gridextra