0

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.

Community
  • 1
  • 1
karlos
  • 873
  • 2
  • 10
  • 21
  • `heights=c(4,1)`; pie charts in ggplot2 are weird, it seems difficult to remove the space entirely – baptiste Oct 01 '14 at 17:27
  • @baptiste yes, pie charts are totally weird in ggplot. I will check the heights option. thanks – karlos Oct 01 '14 at 21:02

1 Answers1

2

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

enter image description here

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!

Community
  • 1
  • 1
SimonG
  • 4,701
  • 3
  • 20
  • 31
  • Although I don't understand all the arguments given to viewports, looks like this will work. Is there a place where I can learn about viewports and how they are used in these packages? – karlos Oct 01 '14 at 21:04
  • You can try playing around with `heights` argument of the layouting command (especially the "4"), and the `ỳ` argument in the last viewport. The layout specifies two plotting regions, one for the plot, and the second one (4 lines of height) for the table. The `y` argument repositions the table upwards (zero being the bottom of the viewport, 1 the top; larger values will draw it closer to the plot above). – SimonG Oct 01 '14 at 21:11