facet_grid with nrow or ncol?

1,532 views
Skip to first unread message

Tengfei Yin

unread,
Feb 16, 2012, 1:12:16 PM2/16/12
to ggplot2
Hi dear all,

I have a general question, is there a way to use facet_grid( A ~ B) with ncol or nrow. or in other words, is it possible to rearrange the panels in facet_grid, and layout by specifying ncol or nrow. 

the case is usefull  when in the formula like A ~ B, where B has 21 levels(lots of levels) and I hope to wrap them in a 7 * 3 grid, in each grid cell, is a small facet_grid panel. 

It's possible to use gtable to get components I need and use grid.arrange to rearrange all the components in lower level, I am wondering if there is any existing function to do the same thing? 

Thanks

Tengfei


--
Tengfei Yin
MCDB PhD student
1620 Howe Hall, 2274,
Iowa State University
Ames, IA,50011-2274



Ben Bolker

unread,
Jul 20, 2012, 12:44:44 PM7/20/12
to ggp...@googlegroups.com

One potential solution might be to create the A:B interaction within
your original data frame (mydata <- transform(mydata,
ABint=interaction(A,B))) and then facet_wrap(~ABint,nrow=...) ... ?
(Rearranging the order of the levels if necessary ...) ?

On 12-07-20 12:03 PM, James wrote:
>
> Did you get a reply for this as I am having a simialr problem
> --
> 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

Jean-Olivier Irisson

unread,
Jul 20, 2012, 1:00:31 PM7/20/12
to Ben Bolker, ggp...@googlegroups.com
On 2012-Jul-20, at 18:44 , Ben Bolker wrote:
>
>
> One potential solution might be to create the A:B interaction within
> your original data frame (mydata <- transform(mydata,
> ABint=interaction(A,B))) and then facet_wrap(~ABint,nrow=...) ... ?
> (Rearranging the order of the levels if necessary ...) ?

You actually don't even need to compute the interaction yourself

facet_wrap(~A+B, ncol=...)

should work.

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/
Send me large files at: http://jo.irisson.com/upload/


James

unread,
Jul 23, 2012, 7:16:17 AM7/23/12
to ggp...@googlegroups.com
perfect...thanks guys. Just discovered facet_grid and didn't think that facet_wrap would work as I only had one variable to seperate the plots (in this case, date).

By using

facet_wrap(~date,ncol=4)

This produced all the graphs in a nice grid opposed to a list format.

Thanks!

James


On Thursday, February 16, 2012 6:12:16 PM UTC, Tengfei wrote:
Reply all
Reply to author
Forward
0 new messages