【问题标题】:Saving plots using grid_draw method instead of gridExtra使用 grid_draw 方法而不是 gridExtra 保存绘图
【发布时间】:2015-10-14 19:40:26
【问题描述】:

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

但是,gridExtra 中的图未对齐,所以我使用了这种方法

 #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)

此方法已在此链接中介绍

The perils of aligning plots in ggplot

它对我的图表很有效,但是当我保存时

object <- grid.draw(g)

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

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

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

您将如何保存 grid.draw 输出?我尝试使用 Rstudio UI 保存它,但它只保存一个情节,而不是两个相邻的情节

【问题讨论】:

    标签: r plot gridextra gtable


    【解决方案1】:

    忘了提到 png ( ) 的宽度和高度将取决于你的情节,所以请随意尝试。

    gtable 对象是这样保存的:

    根据情节,可以调整 png 的尺寸以适合情节

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

    【讨论】:

      【解决方案2】:

      这应该可行。

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

      【讨论】:

        【解决方案3】:
        ggsave('g.png', plot = g, width=6,height=4,dpi=600)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-04-23
          • 2012-03-26
          • 2011-08-14
          • 1970-01-01
          相关资源
          最近更新 更多