减少grid.arrange中两个图之间的空间 [英] Reducing space between two plots in grid.arrange
问题描述
这里是我的示例代码。
< (数量= 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屋!