The basic idea is that you assign ggplot2 plots to an object, and then use the arrange function to display two or more. Here's an example. First copy and paste the code above (or put in your Rprofile). Next install and/or load ggplot2 as described in a previous ggplot2 tutorial.
# Load the diamonds dataset data(diamonds) # Create a histogram, assign to "plot1" plot1 <- qplot(price,data=diamonds,binwidth=1000) # Create a scatterplot plot2 <- qplot(carat,price,data=diamonds) # Arrange and display the plots into a 2x1 grid arrange_ggplot2(plot1,plot2,ncol=1)And here's what you should get:
...
wow that was fast :)
ReplyDeleteAlso, check out imagemagick. It's a cross platform opensource image manipulation tool. It's really handy for stitching multiple images together. But the montage binary in your path, then call:
ReplyDeletemontage image1.png image2.png -geometry -0-0 -tile 1x composite.png
to stitch image1 and image2 together in a single column. Ideally all your images will be the same size. You can do this with lots of images using wildcards.
Great tip! I've been struggling with this aspect of ggplot2 for a while.
ReplyDeleteGreat!! I was looking for something like this just today!
ReplyDeleteI started learning ggplot2 a couple weeks ago, so maybe I'm missing something. After I load the library(ggplot2), arrange() isn't showing up as a function. I tried loading library(ggplot), to see if it was in there, but still no luck.
ReplyDeleteAny idea? ... I've been looking for this solution, but the no-arrange() is getting in the way.
Dan, the arrange function is not included with ggplot2. You have to copy and paste the code in the first box above to get it to work.
ReplyDeletewoohoo! works great! thanks
ReplyDeleteGreat bit of code... But.. is there a way to output to a file?
ReplyDeleteI've tried:-
tmp<-arrange(mh3,mh4,mh1,mh2,ncol=1)
ggsave(file="all.png",tmp)
Thanks in advance
Jennie,
ReplyDeleteI haven't had success with this either. Just use the GUI to save the image as PNG or PDF. I'll post a solution if I find one.
Stephen
to save plots to file
ReplyDeletepdf("your_file.pdf",width = 11, height = 8)
arrange(plot1,plot2,plot3,plot4,ncol=2)
dev.off()
Hi,
ReplyDeleteI get the following error when I do this.
Error: attempt to apply non-function
whats that about?
Hey guys, can you slap a license on your code? (BSD would be highly appreciated)
ReplyDeleteThis comment has been removed by the author.
ReplyDeletenote that this function is included in the gridExtra package, with the name grid.arrange() and a few more options.
ReplyDeleteGreat trick, thanks. How to go about having two plots share the same x-axis?
ReplyDeleteThank you Stephen Turner. It has been great use your code. Thanks.
ReplyDeletehow do you control the space between the plots when using "arrange function"?
ReplyDeletei put your code from above in R and run it and than told him to arrange my plots, but i always get the error message
ReplyDeletecouldn't find function "grid.newpage"
any idea what i could do differently?
Apologies - I updated the code (and renamed the function to arrange_ggplot2). The problem is newer versions of ggplot2 don't automatically load the grid package, so you have to load it manually. I put this in as the first line of code. Give it a shot now.
DeleteThanks Steven! My google searches seem to end up on your blog fairly often with helpful results. I also found this package (http://code.google.com/p/gridextra/) which may be useful as well.
ReplyDeleteThanks for sharing. I modified this code above years ago from something I found in a mailing list or forum. I think eventually this functionality became part of the gridextra package.
DeleteThanks Steven for the code. It´s has been very useful !!
ReplyDeleteOne question. I have a plot of 5x3 and I would like to reduce the blank space between plots. Is it possible?
Thanks
Juan Carlos
I am wondering the same thing here. I see that you cannot simply change
Deletetheme(plot.margin = ...)
is the margin something that is a constant when using this function or is it an function of the plot size? Also how can we reduce this and increase size of plots?
Perfect - Thank you!
ReplyDelete