how to split a plot into multiple pages, or at least mimic so

2,984 views
Skip to first unread message

Giovanni Marco Dall'Olio

unread,
Mar 16, 2010, 11:39:15 AM3/16/10
to ggplot2
Hi,
I would like to split a facetted plot into multiple pages, while
maintaining the same layout in all pages.
For example, let's take the diamonds data:

>>> library(ggplot2)
>>> qplot(data=diamonds, y=price, x=carat, facets=.~color)

This command plots seven facets, each for every value of colors, in an
unique plot... but what if I want to split it so I have two colors per
plot, for example D-E, F-G, H-I, and J in the last page?

The only solution that I could get (thanks to an old post in this
mailing list) is to split the input data into multiple subset and
repeat the plot for each one of them. This does work, but not very
well, because the last page will contain a fewer number of elements
than the others and therefore will have a different layout (see again
the example for the diamond data, in which the last plot will have
only the J color).

On lattice, there is a parameter called layout that you can pass to a
plotting function to tell it to split output to a number of rows,
columns and pages. As far as I know there is no way to do so with
ggplot.. are you planning on implementing this feature soon?
And in the meantime, do you have any idea on how I can get good
results by splitting the input data and plotting it multiple times,
and then call ggplot in order to generate the last page with the same
layout as the other ones? I thought about plotting blank datasets, but
I don't like this idea very much..

--
Giovanni Dall'Olio, phd student
Department of Biologia Evolutiva at CEXS-UPF (Barcelona, Spain)

My blog on bioinformatics: http://bioinfoblog.it

takahashi kohske

unread,
Mar 16, 2010, 10:09:07 PM3/16/10
to dallo...@gmail.com, ggplot2
Hi,

I don't know if this is the best way for ggplot2 :-p,
and I also don't like this kind of hack because I like the
user-friendly drawing system of ggplot2,
but at least you can remove the extra panel after drawing dummy data
via grid.remove as follows:

d<-rbind(diamonds[1,],diamonds)
levels(d$color)<-c(levels(d$color),"Z")
d[1,]$color<-"Z"
q<-ggplot(subset(d,color%in%c("J","Z")),aes(x=carat,y=price))+facet_grid(.~color)+geom_point()
print(q)
grid.remove("panel-3-5")
grid.remove("axis_h-5-5")
grid.remove("strip_h-1-5")

HTH.

2010/3/17 Giovanni Marco Dall'Olio <dallo...@gmail.com>:

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

Giovanni Marco Dall'Olio

unread,
Mar 17, 2010, 10:37:08 AM3/17/10
to takahashi kohske, ggplot2
Thank you very much Takahashi.
It is an hack but it works perfectly.

I have also reported this on github, here is the entry:
- http://github.com/hadley/ggplot2/issuesearch?state=open&q=multiple#issue/83

hadley wickham

unread,
Mar 17, 2010, 6:06:02 PM3/17/10
to dallo...@gmail.com, takahashi kohske, ggplot2
> I have also reported this on github, here is the entry:
> -
> http://github.com/hadley/ggplot2/issuesearch?state=open&q=multiple#issue/83

To help me decide where to place this on my priority list, I need to
know two things:

* do you need this only for facet_wrap, or also for facet_grid?
* how many other people would find this useful?

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Giovanni Marco Dall'Olio

unread,
Mar 17, 2010, 6:45:49 PM3/17/10
to hadley wickham, takahashi kohske, ggplot2
On Wed, Mar 17, 2010 at 11:06 PM, hadley wickham <h.wi...@gmail.com> wrote:
> I have also reported this on github, here is the entry:
> -
> http://github.com/hadley/ggplot2/issuesearch?state=open&q=multiple#issue/83

To help me decide where to place this on my priority list, I need to
know two things:

 * do you need this only for facet_wrap, or also for facet_grid?

Ideally for both.
An idea for an implementation is to create a function called something like 'page_layout' as for other functions that control the layout in ggplot2.

examples:

>>> qplot(carat, quality, data=diamonds) + facets(~color) + page_layout(npages=2)
-> this will split the plot in two pages

>>> qplot(carat, quality, data=diamonds) + facets(~color) + page_layout(columns=4)
-> this will split the plot so to have 4 columns on every page. The number of pages is determined automatically.


In lattice, the same task is accomplished by the layout parameter passed to a plotting function.
This parameter must be a list of three elements: (number of columns, number of rows, number of pages). The last number is optional. Examples:
>>> xyplot(data=diamonds, carat~clarity, layout=c(3,2))
-> plots the data in pages with 3 columns and 2 rows
>>> xyplot(data=diamonds, carat~clarity, layout=c(0,2,3))
-> plots the data on two columns (facets) per page, in order to occupy exactly three pages.

 
 * how many other people would find this useful?

I don't know. For me it would be useful.

 
Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Xie Chao

unread,
Mar 18, 2010, 3:22:35 AM3/18/10
to hadley wickham, dallo...@gmail.com, takahashi kohske, ggplot2
On Thu, Mar 18, 2010 at 6:06 AM, hadley wickham <h.wi...@gmail.com> wrote:
>> I have also reported this on github, here is the entry:
>> -
>> http://github.com/hadley/ggplot2/issuesearch?state=open&q=multiple#issue/83
>
> To help me decide where to place this on my priority list, I need to
> know two things:
>
>  * do you need this only for facet_wrap, or also for facet_grid?
>  * how many other people would find this useful?

I find this useful too. For me multi-page facet_wrap is more useful.

xiechao

>
> Hadley
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>

Dennis Murphy

unread,
Mar 18, 2010, 4:18:35 AM3/18/10
to ggplot2
Considering that datasets aren't getting any smaller, I would think the demand
for multipage plots with a consistent interface will become greater over time.
My 2c worth...

Dennis

ONKELINX, Thierry

unread,
Mar 18, 2010, 4:59:06 AM3/18/10
to hadley wickham, dallo...@gmail.com, takahashi kohske, ggplot2
Hi Hadley,

I would find it very usefull for both facet_grid and facet_wrap. Until
now I did it manually in a production script with facet_wrap with 20 to
100 facets. I need to group them in 4 by 4 layouts to keep the plots
readable.
A nice feature would be that the last page, which will have a smaller
number of facets, has the same facet size as the other pages. So it will
have some blank facets.

Best regards,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

> -----Oorspronkelijk bericht-----
> Van: ggp...@googlegroups.com
> [mailto:ggp...@googlegroups.com] Namens hadley wickham
> Verzonden: woensdag 17 maart 2010 23:06
> Aan: dallo...@gmail.com
> CC: takahashi kohske; ggplot2
> Onderwerp: Re: how to split a plot into multiple pages, or at
> least mimic so

> --
> You received this message because you are subscribed to the
> ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
>

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.

Luciano Selzer

unread,
Mar 18, 2010, 8:48:54 AM3/18/10
to hadley wickham, ggplot2
I would find it usefull since I have to graph several variables and the graphics become very hard to read with a facet grid of 10 * 4.
Luciano


2010/3/17 hadley wickham <h.wi...@gmail.com>

--

hadley wickham

unread,
Mar 18, 2010, 9:24:17 AM3/18/10
to ONKELINX, Thierry, dallo...@gmail.com, takahashi kohske, ggplot2
> I would find it very usefull for both facet_grid and facet_wrap. Until
> now I did it manually in a production script with facet_wrap with 20 to
> 100 facets. I need to group them in 4 by 4 layouts to keep the plots
> readable.

How would you want the layout to work with facet_grid? Would ggplot2
basically layout the entire plot specification and then divide it up
in to pages of specified size? Or would you want to specify the
variable that divided up the pages?

hadley wickham

unread,
Mar 18, 2010, 9:25:10 AM3/18/10
to Dennis Murphy, ggplot2
> Considering that datasets aren't getting any smaller, I would think the
> demand
> for multipage plots with a consistent interface will become greater over
> time.

But multipage plots are pretty crap for data exploration - you're much
better off using linked brushing.

Kasper Daniel Hansen

unread,
Mar 18, 2010, 9:54:37 AM3/18/10
to hadley wickham, Dennis Murphy, ggplot2
On Thu, Mar 18, 2010 at 9:25 AM, hadley wickham <h.wi...@gmail.com> wrote:
>> Considering that datasets aren't getting any smaller, I would think the
>> demand
>> for multipage plots with a consistent interface will become greater over
>> time.
>
> But multipage plots are pretty crap for data exploration - you're much
> better off using linked brushing.

That depends. You could print them and lay them out on your desk.
That gives you a lot of resolution.

Kasper

hadley wickham

unread,
Mar 18, 2010, 11:00:22 AM3/18/10
to Jean-Louis Abitbol, Dennis Murphy
> My other wish would be to be able to use as many shapes as I wish ie
> like in lattice up to 24 (not sure but around that number ) shapes
> available with automatic recycling. If I understand well this is limited
> to 6 in ggplot or you have to manually set-up.

If you can provide evidence that it's possible to reliably distinguish
between those 24 shapes, I'll happily up the limit ;)

paulie p

unread,
Mar 18, 2010, 2:50:11 PM3/18/10
to ggplot2
My vote on this issue was already cast as a "yes" for both facet_wrap
and facet_grid in an earlier posting. In fact, the only reason I
still use lattice at all is for this for the "layout" feature.

And while we're on the topic of laying out faceted plots, (and this
maybe getting head of things a little) is a feature I wish the layout
option had in lattice, namely, a way to create each layout page in its
own window. This would be moral equivalent of laying sheets of paper
on your desk.

Paul

Dennis Murphy

unread,
Mar 18, 2010, 3:53:36 PM3/18/10
to hadley wickham, ggplot2
Two comments:

(1) Multipage layouts aren't just for exploration, but also for presentation in
     reports, books and (possibly, but less likely) journal articles. I'm not a huge
     fan of them myself, but sometimes they're necessary.
(2) I agree with your comment in principle re linked brushing, but it takes a bit of
     time to gain facility with either iplots or the ggobi interface through rggobi, just
     as it does in ggplot2. I've been unable in the past (approximately a year ago)
     to generate hardcopy graphics from interesting views found in ggobi, which was
     rather frustrating at the time. I understand that's been fixed now (in DescribeDisplay);
     any example code available to illustrate its use in conjunction with rggobi?

         
Dennis

Reinhold

unread,
Mar 19, 2010, 11:50:09 AM3/19/10
to ggplot2
I use facet_grid a lot for clinical data; a typical example is to plot
several lab parameters in different rows and patients into separate
columns. More than 6-8 patients per page is no longer legible.

Interface could simply be that number of columns and rows can
simultaneously be specified.

Best regards
Reinhold

hadley wickham

unread,
Mar 21, 2010, 6:45:27 PM3/21/10
to Jean-Louis Abitbol
Hi Jean-Louis,

It's pretty easy to make your own:

many_shapes <- scale_shape_manual(values = 1:24)

Hadley

On Thu, Mar 18, 2010 at 2:32 PM, Jean-Louis Abitbol
<ggp...@googlegroups.com> wrote:
> I can see your point.
>
> However having many shapes available is useful when you don't have a
> color printer as it helps identifying individual (patients in my case)
> values of scores*time for example. Varying linetype (even more difficult
> to distinguish) and color is not in fact enough in BW.
>
> It is very much readable when you have 10/15 patients and depending on
> the data you can jitter on the x axis.
>
> I use it even when I have 100+ lines (and it may even be more useful in
> that setting) as it also helps identifying profile of patients when you
> don't have color available. In that setting I don't care that I can make
> the correspondance between a line and a given patient. I'll sort it out
> based on the values. So it is important that the shapes are recycled
> automatically as they are in lattice.
>
> Finally I would think it is up to me to decide if I can reliably
> distinguish... I have dozens of examples available but all with
> confidential data. I could think of making a reproducible example but it
> all so much depends on the data that I don't think this would be very
> meaningful.
>
> This is just a humble wish and I think this is not a matter where you
> should limit the users choice unless it implies major coding work . It
> would be more important to forbid pie plots/bar graphs (ink/data ratio!)
> etc... which obviously  you have not done.
>
> Anyway thanks again for your work.
>
> Best wishes, JL

>> --
>> You received this message because you are subscribed to the ggplot2
>> mailing list.
>> To post to this group, send email to ggp...@googlegroups.com
>> To unsubscribe from this group, send email to
>> ggplot2+u...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/ggplot2
>>

> Jean-Louis Abitbol, MD
> Chief Medical Officer
> Trophos SA, Parc scientifique de Luminy, Case 931
> Luminy Biotech Entreprises
> 13288 Marseille Cedex 9 France
> Email: jlab...@trophos.com ---- Backup Email: abi...@sent.com
> Cellular: (33) (0)6 24 47 59 34
> Direct Line: (33) (0)4 91 82 82 73-Switchboard: (33) (0)4 91 82 82 82
> Fax: (33) (0)4 91 82 82 89

hadley wickham

unread,
Mar 21, 2010, 6:48:55 PM3/21/10
to ggplot2
On Wed, Mar 17, 2010 at 5:06 PM, hadley wickham <h.wi...@gmail.com> wrote:
>> I have also reported this on github, here is the entry:
>> -
>> http://github.com/hadley/ggplot2/issuesearch?state=open&q=multiple#issue/83
>
> To help me decide where to place this on my priority list, I need to
> know two things:
>
>  * do you need this only for facet_wrap, or also for facet_grid?
>  * how many other people would find this useful?

Ok, it seems like this is of general interest and I'll add it to my to do list.

The interface will probably be something like page.layout = c(rows, cols)

Tobias Verbeke

unread,
Mar 17, 2014, 11:45:26 AM3/17/14
to Fatman, ggp...@googlegroups.com
+1 for such a feature


On Tue, Jan 15, 2013 at 11:03 PM, Fatman <hath...@gmail.com> wrote:
Is there an update to this setting.
--
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

Alex Gray

unread,
Feb 9, 2015, 4:05:40 PM2/9/15
to ggp...@googlegroups.com
Has this been implemented? It would be really useful. 

Brandon Hurr

unread,
Feb 10, 2015, 11:03:06 AM2/10/15
to Alex Gray, ggplot2
If it hasn't been implemented and it takes more than a few changes to the base code it is unlikely to happen. 

See Hadley's comment from a year ago for why: 

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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

map.2...@gmail.com

unread,
Dec 10, 2015, 7:37:14 PM12/10/15
to ggplot2, ma...@alexwgray.com
This would be a really helpful new feature!

Jiajia Liu

unread,
Jun 2, 2016, 5:03:34 AM6/2/16
to ggplot2, dallo...@gmail.com
https://github.com/guiastrennec/ggplus


I found this package from github that solve this problem called ggplus. 

Jiajia Liu

unread,
Jun 2, 2016, 5:03:34 AM6/2/16
to ggplot2, dallo...@gmail.com
ggplus

facet_multiple can accomplish this!!!



On Wednesday, March 17, 2010 at 4:39:15 AM UTC+13, Giovanni Marco Dall'Olio wrote:
Reply all
Reply to author
Forward
0 new messages