grid.arrange() and ggsave()

5,059 views
Skip to first unread message

Agustin Lobo

unread,
Apr 20, 2011, 2:38:20 PM4/20/11
to ggp...@googlegroups.com
When I use ggsave() after grid.arrange(), i.e.
> grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2)
> ggsave("sgcirNIR.jpg")

I do not save the grid plot but the last individual ggplot. Is there any
way of actually saving the plot as displayed by grid.arrange using
ggsave() or something similar?
(besides using the older way
> jpeg("sgcirNIR.jpg")
> grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2)
> dev.off()
)

Thanks

Agus

Brandon Hurr

unread,
Apr 20, 2011, 3:06:03 PM4/20/11
to Agusti...@ija.csic.es, ggp...@googlegroups.com
I believe that it has to be done the "older way" since you're actually plotting multiple ggplot objects simultaneously and not one large object with multiple components (acceptable input for ggsave). 

I'd love for someone to correct me. 

Brandon


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

baptiste auguie

unread,
Apr 20, 2011, 4:47:28 PM4/20/11
to Brandon Hurr, Agusti...@ija.csic.es, ggp...@googlegroups.com
The following should work with a recent version of gridExtra (>=0.8 I think).

p <- arrangeGrob(qplot(1,1), textGrob("test"))
grid.draw(p) # interactive device

ggsave("saving.pdf", p) # need to specify what to save explicitely

I could add the following line to grid.arrange,

ggplot2:::.store$set(arrangeGrob(...))

so that last_plot() returns the collection of grid objects and your
initial attempt would work, but it seems wrong to me; last_plot()
should probably only return the last ggplot, nothing else. I'm happy
to be convinced otherwise though.

baptiste

baptiste

Agustin Lobo

unread,
Apr 28, 2011, 3:17:47 AM4/28/11
to baptiste auguie, Brandon Hurr, ggp...@googlegroups.com
You are probably right, but then we would perhaps need another function
(i.e., ggridsave()) to save what we have in the graphic window as it is in one
single command:
> grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2)
> ggridsave("sgcirNIR.jpg")

Agus

2011/4/20 baptiste auguie <bapt...@googlemail.com>:

baptiste auguie

unread,
Apr 28, 2011, 4:39:36 AM4/28/11
to Agusti...@ija.csic.es, ggp...@googlegroups.com
Doing so would mean that gridExtra would replicate the code in ggsave
and last_plot with very minor changes, which is never good. I would
argue that a better solution would be to extract the part of ggsave
that isn't specific to ggplot2 and place it in a more generic package
that could be accessed by all grid-related packages. Not sure where
though, since R-base probably wouldn't want the burden, and ggplot2
would require it as a dependency.

saludos,

baptiste

Reply all
Reply to author
Forward
0 new messages