使用 grid_draw 方法而不是 gridExtra 保存绘图 [英] Saving plots using grid_draw method instead of gridExtra

查看:197
本文介绍了使用 grid_draw 方法而不是 gridExtra 保存绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 gridExtra 创建了 2 个彼此相邻的图,我可以使用 ggsave 保存对象

I have used gridExtra to create 2 plots next to each other and I can save the object using ggsave

然而,绘图在 gridExtra 中未对齐,所以我使用了这种方法

However, the plots are misaligned in gridExtra so I used this method

 #Method 2 - gtable
 require(gtable)
 #Extract Grobs
 g1<-ggplotGrob(left)
 g2<-ggplotGrob(right)
 #Bind the tables
  g<-gtable:::cbind_gtable(g1, g2, "first")
 #Remove a row between the plots
 g <- gtable_add_cols(g, unit(-1,"cm"), pos=ncol(g1))
 #draw
 grid.newpage()
 grid.draw(g)

此链接包含此方法

在 ggplot 中对齐图的危险

它对我的图表效果很好,但是当我保存

It worked beautifully for my graphs but when I save the

object <- grid.draw(g)

当我尝试查看对象时,该对象为 NULL,但我不确定如何将其另存为 png

the object is NULL when I try to look at it and I am not sure how to save it as a png

gridExtra 方法可以保存为对象并使用

the gridExtra method can be saved as an object and saved using

ggsave('g.png',width=6,height=4,dpi=600)

你将如何保存 grid.draw 输出?我尝试使用 Rstudio UI 保存它,但它只保存一个图,而不是彼此相邻

how would you save grid.draw output? I tried saving it using Rstudio UI but it only saves one plot not both next to each other

推荐答案

忘记提到 png ( ) 的宽度和高度将取决于您所拥有的情节,因此请随意使用它.

Forgot to mention that width and height of png ( ) will depend on the plot you have so play around with it.

gtable 对象是这样保存的:

This is how gtable object is saved:

根据情节,可以调整 png 的尺寸以适当地适应情节

depending on the plot, the dimensions of the png can be adjusted to properly fit the plot

png("g.png",width = 1000, height = 600, units = "px") 
grid.draw(g) 
dev.off()

这篇关于使用 grid_draw 方法而不是 gridExtra 保存绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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