Saving a grid.arrange plot

6,144 views
Skip to first unread message

Manuel Spínola

unread,
Feb 17, 2011, 5:26:39 PM2/17/11
to ggplot2
Dear list members,

I am trying to save a grid.arrange plot:

> x11(10,6)
> grid.arrange(p1, p2, nrow=1) # p1 and p2 are plots
> ggsave(file = "efecto.png", dpi = 400)
Saving 7" x 7" image

But the saved plot is only one of them, not the 2 plots that I am trying to save in the same plot window.

Any hint on how to do it?

Best,

Manuel


--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

Scott Chamberlain

unread,
Feb 17, 2011, 5:33:04 PM2/17/11
to Manuel Spínola, ggplot2

I would guess something like this: 


pdf("yourplot.pdf") # or jpeg, etc.

yourplot

dev.off() 


Scott

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

Erik Iverson

unread,
Feb 17, 2011, 5:36:45 PM2/17/11
to Manuel Spínola, ggplot2
Not tested, but try:

p3 <- grid.arrange(p1, p2, nrow=1) # p1 and p2 are plots
ggsave(file = "efecto.png", plot = p3, dpi = 400)

or the traditional way:

png(...)

grid.arrange(...)

dev.off()

Manuel Sp�nola wrote:
> Dear list members,
>
> I am trying to save a grid.arrange plot:
>
> > x11(10,6)
> > grid.arrange(p1, p2, nrow=1) # p1 and p2 are plots
> > ggsave(file = "efecto.png", dpi = 400)
> Saving 7" x 7" image
>
> But the saved plot is only one of them, not the 2 plots that I am trying
> to save in the same plot window.
>
> Any hint on how to do it?
>
> Best,
>
> Manuel
>
>
> --

> *Manuel Sp�nola, Ph.D.*
> Instituto Internacional en Conservaci�n y Manejo de Vida Silvestre


> Universidad Nacional
> Apartado 1350-3000
> Heredia
> COSTA RICA
> mspi...@una.ac.cr
> mspin...@gmail.com

> Tel�fono: (506) 2277-3598
> Fax: (506) 2237-7036
> Personal website: Lobito de r�o
> <https://sites.google.com/site/lobitoderio/>
> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>

baptiste auguie

unread,
Feb 18, 2011, 3:42:15 AM2/18/11
to ggplot2, hadley wickham
Hi,

I wonder if it could be desirable to extend ggsave to a wider class of
objects. For instance, one can do the following trick,

- add a class to the compound object (Grid-based, obviously)

- define a print method

ggsave checks that the object derives from the class ggplot, and later
print()s it (print options are not implemented below),

library(ggplot2) ; library(gridExtra)

g = arrangeGrob(qplot(1:10, 1:10), tableGrob(head(iris)))
g2 = gTree(children = gList(g), cl=c("arrange", "ggplot"))
print.arrange = function(x, ...) grid.draw(x)

ggsave("test.pdf", g2)

Just a thought anyway,

baptiste

On Thu, Feb 17, 2011 at 11:36 PM, Erik Iverson <er...@ccbr.umn.edu> wrote:
> Not tested, but try:
>
> p3 <- grid.arrange(p1, p2, nrow=1) # p1 and p2 are plots
> ggsave(file = "efecto.png", plot = p3, dpi = 400)
>
> or the traditional way:
>
> png(...)
>
> grid.arrange(...)
>
> dev.off()
>

> Manuel Spínola wrote:
>>
>>  Dear list members,
>>
>> I am trying to save a grid.arrange plot:
>>
>>  > x11(10,6)
>>  > grid.arrange(p1, p2, nrow=1) # p1 and p2 are plots
>>  > ggsave(file = "efecto.png", dpi = 400)
>> Saving 7" x 7" image
>>
>> But the saved plot is only one of them, not the 2 plots that I am trying
>> to save in the same plot window.
>>
>> Any hint on how to do it?
>>
>> Best,
>>
>> Manuel
>>
>>
>> --

>> *Manuel Spínola, Ph.D.*
>> Instituto Internacional en Conservación y Manejo de Vida Silvestre


>> Universidad Nacional
>> Apartado 1350-3000
>> Heredia
>> COSTA RICA
>> mspi...@una.ac.cr
>> mspin...@gmail.com

>> Teléfono: (506) 2277-3598
>> Fax: (506) 2237-7036
>> Personal website: Lobito de río

Manuel Spínola

unread,
Feb 18, 2011, 6:54:21 AM2/18/11
to ggplot2
Thank you very much for the responses.

When I tried this:

> p3 = grid.arrange(p1, p2, nrow=1)
> ggsave(file = "modelo1.png", plot = 3, dpi = 400)
Error in ggsave(file = "modelo1.png", plot = 3, dpi = 400) :
  plot should be a ggplot2 plot

Does it mean that ggExtra is no producing a ggplot2 plot?

However this works:

png(file = "modelo1.png", width = 800, height = 480)
grid.arrange(p1, p2, nrow=1)
dev.off()

Best,

Manuel


On 17/02/2011 04:36 p.m., Erik Iverson wrote:
Not tested, but try:

p3 <- grid.arrange(p1, p2, nrow=1) # p1 and p2 are plots
ggsave(file = "efecto.png", plot = p3, dpi = 400)

or the traditional way:

png(...)

grid.arrange(...)

dev.off()

Manuel Spínola wrote:
  Dear list members,

I am trying to save a grid.arrange plot:

 > x11(10,6)
 > grid.arrange(p1, p2, nrow=1) # p1 and p2 are plots
 > ggsave(file = "efecto.png", dpi = 400)
Saving 7" x 7" image

But the saved plot is only one of them, not the 2 plots that I am trying to save in the same plot window.

Any hint on how to do it?

Best,

Manuel


-- 
*Manuel Spínola, Ph.D.*
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río <https://sites.google.com/site/lobitoderio/>
Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>

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



--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

baptiste auguie

unread,
Feb 18, 2011, 7:38:54 AM2/18/11
to Manuel Spínola, ggplot2
On Fri, Feb 18, 2011 at 12:54 PM, Manuel Spínola <mspin...@gmail.com> wrote:
> Thank you very much for the responses.
>
> When I tried this:
>
>> p3 = grid.arrange(p1, p2, nrow=1)
>> ggsave(file = "modelo1.png", plot = 3, dpi = 400)
> Error in ggsave(file = "modelo1.png", plot = 3, dpi = 400) :
>   plot should be a ggplot2 plot
>
> Does it mean that ggExtra is no producing a ggplot2 plot?
>

grid.arrange() is from gridExtra not ggExtra, and yes, the output
doesn't belong to the class ggplot.

Manuel Spínola

unread,
Feb 18, 2011, 7:52:58 AM2/18/11
to baptiste auguie, Dennis Murphy, ggplot2
Thank you very much Baptiste and Dennis.

Is there any function that allow me align 2 plots in a row in ggExtra?

Best,

Manuel

baptiste auguie

unread,
Feb 18, 2011, 8:35:56 AM2/18/11
to Manuel Spínola, ggplot2
I've just committed a change to grid.arrange() that should detect the
presence of a ggplot and make the output compatible with ggsave.

Feel free to try it out,

library(ggplot2)
source("http://gridextra.googlecode.com/svn/trunk/R/arrange.r")

g1 = rectGrob(gp=gpar(fill="grey"))
g2 = textGrob("test")
g3 = qplot(1:10, 1:10)


grid.arrange(g1,g2,g3)

g4 = arrangeGrob(g1,g2,g3, main = "testing grid.arrange()")
g5 = arrangeGrob(g1,g2), main = "testing grid.arrange()")

ggsave("test.pdf", g4)

## this fails, no ggplot present
ggsave("test2.pdf", g5)
# Error in ggsave("test2.pdf", g5) : plot should be a ggplot2 plot

baptiste


On Fri, Feb 18, 2011 at 1:52 PM, Manuel Spínola <mspin...@gmail.com> wrote:
> Thank you very much Baptiste and Dennis.
>
> Is there any function that allow me align 2 plots in a row in ggExtra?

No. grid.arrange(..., nrow=1) might be OK if you don't need the y-axes
to be exactly aligned. You could look at the code in align.plots(), it
wouldn't be too hard to do the same for horizontal layouts. (Since
Hadley has been working on a rewrite of the package, I feel lazy to
work on such ugly hacks).

ddeb...@userzoom.com

unread,
Oct 8, 2015, 9:46:59 PM10/8/15
to ggplot2
You could use cowplot and then ggsave. 

plot_grid(p1,p2,rows = 1) 
ggsave(...
Reply all
Reply to author
Forward
0 new messages