减少grid.arrange中两个图之间的空间 [英] Reducing space between two plots in grid.arrange

查看:387
本文介绍了减少grid.arrange中两个图之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我一直在与这个问题作斗争。我想使用grid.arrange减少ggplot和表格之间的空间。我查看了各种线程,在网络上搜索,尝试了各种各样的东西,但如果它们在一列中,我不能缩小两个地块之间的空间。 (是的,它需要是饼图,尽管表提供了更好的信息)

这里是我的示例代码。



< (数量= c(200,500))
g < - ggplot(Type = c水果,aes(x = factor(1),y = Quantity,fill = Type))+ geom_bar(position =fill,stat =identity,width = 1)+ coord_polar(theta =y)
g <-g + labs(x = NULL,y = NULL)+ theme(axis.ticks = element_blank(),axis.text = element_blank(),panel.background = element_rect(fill =white,color = (c(0,0,0,0),lines))
(plot,white),legend.position =none)
g < - g + theme gtablegrob < - tableGrob(fruits,show.rownames = FALSE,gpar.coretext = gpar(fontsize = 10),par.coltext = gpar(fontsize = 10),gpar.rowtext = gpar(fontsize = 10),gpar.corefill = gpar(fill =white,col =white))
grid.arrange(g,gtablegrob,nrow = 2)

另一个类似的线程提供了解决方案您可以尝试使用网格,但是我无法找到有关高度的文档。 包。请参阅下面的png输出示例。在这个
中有类似的问题要求询问

 库(ggplot2)
库(网格)

#layout
vp.layout< - grid.layout(nrow = 2,ncol = 1,heights = unit(c(1,4),c(null,lines)) ,
widths = unit(1,null))

png(test.png,width = 200,height = 350)
#开始绘图
grid.newpage()
pushViewport(viewport(layout = vp.layout,name =layout))
#plot
pushViewport(viewport(layout.pos.row = 1,layout .pos.col = 1,name =plot))
print(g,newpage = FALSE)
upViewport()
#table
pushViewport(viewport(layout.pos .row = 2,layout.pos.col = 1,name =table))
pushViewport(viewport(y = unit(1.2,npc),name =tableloc))
grid.draw(gtablegrob)
upViewport()
dev.off()



然而,由于tableGrob不允许使用许多关于字体的缩放选项,我建议使用pdf来代替,这是可缩放的,即使初始图形相当小。 开罗包在这方面做得很好。



干杯!


I have been battling with this problem for sometime now. I want to reduce the space between a ggplot and a table using grid.arrange. I looked at various threads, searched on the web, tried various things, but I cannot reduce the space between two plots if they are in one column. (yes, it needs to be a pie chart though a table provides better information)

Here's my sample code.

fruits <- data.frame(Type = c("Oranges", "Apples"), Quantity = c(200, 500))
g <- ggplot(fruits, aes(x = factor(1), y = Quantity, fill = Type)) + geom_bar(position = "fill", stat = "identity", width = 1) + coord_polar(theta = "y") 
g <- g  + labs(x = NULL, y = NULL) +  theme(axis.ticks = element_blank(), axis.text = element_blank(), panel.background = element_rect(fill = "white", colour = "white"), legend.position = "none")
g <- g + theme(plot.margin = unit(c(0,0,0,0), "lines"))
gtablegrob <- tableGrob(fruits, show.rownames = FALSE, gpar.coretext = gpar(fontsize = 10), par.coltext = gpar(fontsize = 10), gpar.rowtext = gpar(fontsize = 10), gpar.corefill = gpar(fill = "white", col = "white")) 
grid.arrange(g, gtablegrob, nrow = 2)

The other similar thread provides solution on widths, but I could not find documentation on heights.

解决方案

You can try using the grid package. See below example for png-output. A similar question has been asked in this post.

library(ggplot2)
library(grid)

# layout
vp.layout <- grid.layout(nrow=2, ncol=1, heights=unit(c(1,4), c("null","lines")),
  widths=unit(1,"null") )

png("test.png", width=200, height=350)
# start drawing
grid.newpage()
pushViewport(viewport(layout=vp.layout, name="layout"))
# plot
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1, name="plot"))
print(g, newpage=FALSE)
upViewport()
# table
pushViewport(viewport(layout.pos.row=2, layout.pos.col=1, name="table"))
pushViewport(viewport(y=unit(1.2,"npc"), name="tableloc"))
grid.draw(gtablegrob)
upViewport()
dev.off()

However, since the tableGrob doesn't allow for many scaling options regarding the fonts, I would advise to use pdf instead, which is scalable even if the initial graphic is rather small. The Cairo package does a great job on that.

Cheers!

这篇关于减少grid.arrange中两个图之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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