Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using ggsave and arrangeGrob after updating gridExtra to 2.0.0

since I read a lot similar question on stackoverflow so far, I couldn't find a good solution without updating ggplot2 to the development version.

My problem, I have several scripts which use arrangeGrob to create combined graph out of individual graphs. I save them into a variable and print this variable and/or save it with ggsave. Since a lot of my colleagues update there packages regularly (which is a good thing I think), I always get mails my script no longer work after updating to gridExtra 2.0.0.

I am not sure how to handle this, since the new ggplot2 version where the problem is solved is still under development. I found an article on stack overflow to remove a test if the object to save is a ggplot since the new arrangeGrob function returns a gtable object, but this fails in my case:

library(ggplot2)
library(grid)
library(gridExtra)
a <- data.frame(x=c(1,2,3),
                y=c(2,3,4))
p <- ggplot(a, aes(x, y)) + geom_point()
b <- arrangeGrob(p, p)
grid.draw(b)
ggsave('test.pdf', b)
ggsave <- ggplot2::ggsave
body(ggsave) <- body(ggplot2::ggsave)[-2]
ggsave('test.pdf', b)

Some output and error on the console:

d> grid.draw(b)
d> ggsave('test.pdf', b)
Error in ggsave("test.pdf", b) : plot should be a ggplot2 plot
d> ggsave <- ggplot2::ggsave
d> body(ggsave) <- body(ggplot2::ggsave)[-2]
d> ggsave('test.pdf', b)
Saving 10.5 x 10.7 in image
TableGrob (2 x 1) "arrange": 2 grobs
  z     cells    name           grob
1 1 (1-1,1-1) arrange gtable[layout]
2 2 (2-2,1-1) arrange gtable[layout]
d> 

The test.pdf is created but it is corrupted in any way and can not be opened. Also the gtable object get printed. So I guess something is wrong here.

But, as you can see, I found in the example code, I found the grid.draw function to plot at least my combined graph but I still can not ggsave it after the modification.

I don't want to use the "old" (pdf(file = "test.pdf"); grid.draw(b); dev.off()) device saving functions as suggested in this article, since they are very uncomfortable to use.

In this question someone asked exactly how to save the object, but in the answer they just explain to use grid.darw and he accepted the answer as solving the problem and nobody answered on my comments so far.

So I am pretty lost at the moment, how to provide working scripts for those who have and have not updated to new gridExtra package. The way to remove the test within the ggsave function is I guess the best solution since I can check the gridExtra and ggplot2 version and just overwrite the ggsave function in case there version do not match, but I could not get it to run.

Looking forward to get some help.

EDIT:

maybe the sessionInfo helps

d> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gridExtra_2.0.0 ggplot2_1.0.1  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.1      digest_0.6.8     MASS_7.3-44      plyr_1.8.3       gtable_0.1.2    
 [6] magrittr_1.5     scales_0.3.0     stringi_1.0-1    reshape2_1.4.1   devtools_1.9.1  
[11] proto_0.3-10     tools_3.2.0      stringr_1.0.0    munsell_0.4.2    colorspace_1.2-6
[16] memoise_0.2.1  
like image 988
drmariod Avatar asked Nov 20 '15 09:11

drmariod


1 Answers

As an temporary workaround for this unfortunate transition period, you could re-implement the class hack that used to be in gridExtra,

class(b) <- c("arrange","ggplot", class(b))
print.arrange <- function(x) grid.draw(x)
ggsave('test.pdf', b)
like image 58
baptiste Avatar answered Oct 14 '22 02:10

baptiste