Question

I am combining multip ggplot graphs using gridExtra package grid.arrange function.

I doing this:

p1<-ggplot(x, aes(Date, Value)) + geom_line()
p2<-ggplot(y, aes(Date, Score)) + geom_point()
grid.arrange(p1, p2,  main=textGrob("Head Line", gp=gpar(cex=1.5, fontface="bold", col="#990000")), ncol = 1, clip=TRUE)

this command puts border between p1 and p2. I couldn't find any info on removeing the borders in grid.arrange. Is it possible to remove the borders?

Était-ce utile?

La solution

gridExtra doesn't put any additional border between the plots. All you are seeing are the borders that already surround each plot. That is, there is a border at the bottom of p1 and a border at the top of p2. Put the two together, and it might look like there is additional space between the two.

To remove or to adjust each plot's borders, use the plot.margin element in the theme function. The following removes the bottom margin of p1 and the top margin of p2.

library(ggplot2)
library(gridExtra)

p1<-ggplot(data.frame(x = 1:10, y = 1:10), aes(x, y)) + geom_line() +
      theme(plot.margin = unit(c(1,1,0,1), "lines"))

p2<-ggplot(data.frame(x = 1:10, y = 1:10), aes(x, y)) + geom_point() +
   theme(plot.margin = unit(c(0,1,1,1), "lines"))

grid.arrange(p1, p2,  top=textGrob("Head Line", 
     gp=gpar(cex=1.5, fontface="bold", col="#990000")), ncol = 1, clip=TRUE)

enter image description here

Edit (16/07/2015): with gridExtra >= 2.0.0, the main parameter has been renamed top.

Autres conseils

A little late but I had the same problem and I think I've found the solution. plot.margin didn't help, but adjusting panel.border and plot.background (arguments color and fill) to the background color of the plot did the trick.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top