ggsave with multiple plots - only final plot saved

3,391 views
Skip to first unread message

skipperhoyer

unread,
Oct 7, 2012, 10:54:54 AM10/7/12
to ggp...@googlegroups.com
Hi

I have arranged two plots into one window using multiplot(), see link below. When I save this using ggsave(), it only saves the final plot. Using pdf() ... dev.off() works fine.  Is there a way to do this fully within ggplot2?

MWE:
library(ggplot2)
# require: load multiplot() from: http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/
dat <- data.frame(x = 1:10, y = 1:10)
p1 <- ggplot(dat, aes(x = x, y = y)) + geom_point(col="black", size = 4)
p2 <- ggplot(dat, aes(x = x, y = y)) + geom_point(col="red", size = 4)
multiplot(p1, p2)
ggsave("test1.pdf")    #Only saves p2
pdf("test2.pdf")
multiplot(p1, p2)
dev.off()                   #Saves everything - but seems a bit hacky

Added info: the plots are not saved on multiple pages, there really is just one plot. I guess it's because ggsave grabs "last_plot()", and multiplot plots each element separately.

Thanks!
Benjamin

Brandon Hurr

unread,
Oct 7, 2012, 10:58:32 AM10/7/12
to skipperhoyer, ggp...@googlegroups.com
Sadly, this doesn't work with ggsave currently because it will simply take the last plot in your case p2 and then plot it. It does not recognize grid object that is assembled by multiplot(). When you save you will have to do what you've been doing png()/pdf(); dev.off() for the foreseeable future. There have been mutterings that the ability to do what you want is on the way, but I think it is fairly low priority at the moment. 

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

baptiste auguie

unread,
Oct 7, 2012, 2:46:22 PM10/7/12
to Brandon Hurr, skipperhoyer, ggp...@googlegroups.com

arrangeGrob can do that if you pass the grob to ggsave explicitly.

b.

sent from my phone

On 8/10/2012 3:58 AM, "Brandon Hurr" <brando...@gmail.com> wrote:

Sadly, this doesn't work with ggsave currently because it will simply take the last plot in your case p2 and then plot it. It does not recognize grid object that is assembled by multiplot(). When you save you will have to do what you've been doing png()/pdf(); dev.off() for the foreseeable future. There have been mutterings that the ability to do what you want is on the way, but I think it is fairly low priority at the moment. 



On Sun, Oct 7, 2012 at 3:54 PM, skipperhoyer <bdh...@gmail.com> wrote:
>
> Hi
>

> I have arranged...

Reply all
Reply to author
Forward
0 new messages