baptiste / gridExtra

Miscellaneous Functions for "Grid" Graphics
http://cran.r-project.org/web/packages/gridExtra/index.html
15 stars 4 forks source link

Blank first page created by marrangeGrob if facets used in ggplot #33

Closed kotliary closed 8 years ago

kotliary commented 8 years ago

Here is a minimal reproducible example:

require(dplyr)
require(ggplot2)
require(gridExtra)

p=list()
df = mtcars %>%
  group_by(gear, carb) %>%
  summarise(n=n()) %>%
  ungroup() %>%
  mutate(gear=factor(gear), carb=factor(carb))
p[[1]] = ggplot(df, aes(carb, n, fill=carb)) + 
  facet_wrap(~gear, nrow=1) +
  geom_bar(stat="identity")

ggsave("testm.pdf", marrangeGrob(grobs = p, nrow=1, ncol=1, top=NULL))
ggsave("test.pdf", arrangeGrob(grobs = p))

I get testm.pdf with first blank page, but test.pdf is without. If facet_wrap line is commented, both files are similar.

baptiste commented 8 years ago

There does seem to be a problem, but honestly I've given up chasing breakage caused by changes in gtable and ggplot2. This might work for you,

grid.draw.arrangelist <- function(x, ...) {
  for(ii in seq_along(x)){
    if(ii>1) grid.newpage() 
     grid.draw(x[[ii]])
}
}
dmckean commented 7 years ago

@baptiste that fixed it for me