在R中,如何存储一个插图图形以便稍后使用grid.arrange进行排列? [英] In R, how can I store an inset graph for later arranging it with grid.arrange?

查看:793
本文介绍了在R中,如何存储一个插图图形以便稍后使用grid.arrange进行排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个图表,其中我通过此命令插入了另一个图表(两个ggplot2对象):

  vp = viewPort (...)
print(ggplotobject1)
print(ggplotobject2,vp = vp)

这完全符合我的想法(在viewPort中指定的区域中绘制一个自定义小图的大图)。



问题是我需要稍后使用这个组合图来再次与其他图重新排列:

  grid.arrange(arrangeGrob(.. ))

有没有人知道如何将我的组合图存储为grob?



确实非常感谢!

编辑:
响应到baptiste这里是一个可重现的例子:

pre $ library $ g
$ g
$网格)

数据< -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

非常感谢!

解决方案

您可以使用 annotation_custom 或编辑 gtable ,而不是打印到不同的视口。



<$ (gtable)
库(网格)$ g $ p $ g $ p $ g $ p $ g $ p $ g $
panel.id< - 1
panel< - gm $ layout [gm $ layout $ name ==panel,] [panel.id,]

inset < - grobTree(gs,vp = viewport(width = unit(1,in),x = 0.8,y = 0.8,
height = unit(1,in))
gm < - gtable_add_grob(gm,inset,l = panel $ l,t = panel $ t)
grid.newpage()
grid.draw(gm)


I have created a graph in which I inset another graph (both ggplot2 objects) via this command:

    vp=viewPort(...)
    print(ggplotobject1)
    print(ggplotobject2, vp=vp)

This works exactly how I'd like it to (one large graph with a custom small graph drawn in the area specified in viewPort).

The problem is that I need to use this combined graph later for arranging it with other plots again through:

grid.arrange(arrangeGrob(..))

Does anyone have an idea how I can store my combined graph as a grob?

Thank you very much indeed!

EDIT: Responding to baptiste here is a reproducible example:

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)

This produces the graph I want, but I fail to make the subplot the right size (it's supposed to be a small graph in the bottom left corner of the plot). This is probably really basic, but I haven't worked with gtable before and only a little bit with grid/gridExtra.

Thanks a lot!

解决方案

you could use annotation_custom or edit the gtable instead of printing to different viewports.

gm <- ggplotGrob(main_plot)
gs <- ggplotGrob(sub_plot)

library(gtable)
library(grid)
panel.id <- 1
panel <- gm$layout[gm$layout$name == "panel",][panel.id,]

inset <- grobTree(gs, vp=viewport(width=unit(1,"in"), x=0.8, y=0.8,
                                  height=unit(1,"in")))
gm <- gtable_add_grob(gm, inset, l=panel$l, t=panel$t)
grid.newpage()
grid.draw(gm)

这篇关于在R中,如何存储一个插图图形以便稍后使用grid.arrange进行排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆