Saving plots using grid_draw method instead of gridExtra

本小妞迷上赌 提交于 2019-12-22 09:46:24

问题


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

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)

this method is covered in this link

The perils of aligning plots in ggplot

It worked beautifully for my graphs but when I save the

object <- grid.draw(g)

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

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

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

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


回答1:


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

This is how gtable object is saved:

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



回答2:


This should work.

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)


来源:https://stackoverflow.com/questions/33134163/saving-plots-using-grid-draw-method-instead-of-gridextra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!