Multi-page plotting

22 views
Skip to first unread message

Elaine

unread,
Mar 24, 2009, 12:45:47 PM3/24/09
to ggplot2
I have been searching for a way to split my plots into multiple
pages. I often have too many facets to easy view on one page, and I'd
like to be able to specify how many should go on each page, causing
the rest to spill onto the next page. Is there a way to do this in
ggplot? Right now I'm subsetting the data in order to do this, but
this is not desireable for several obvious reasons . . .

Elaine

Statistician
BD Technologies
Research Triangle Park

hadley wickham

unread,
Mar 24, 2009, 5:06:01 PM3/24/09
to Elaine, ggplot2
Hi Elaine,

On Tue, Mar 24, 2009 at 11:45 AM, Elaine <elaine...@gmail.com> wrote:
>
> I have been searching for a way to split my plots into multiple
> pages.  I often have too many facets to easy view on one page, and I'd
> like to be able to specify how many should go on each page, causing
> the rest to spill onto the next page.  Is there a way to do this in
> ggplot?  Right now I'm subsetting the data in order to do this, but
> this is not desireable for several obvious reasons . . .

I currently don't have any plans to support multiple pages, but they
are quite easy to do yourself. Here's some example code I sent to
someone else who was interested in multiple pages of output:

# Assuming your data frame is called df and your patient identifier is
patient_id
df$group <- as.numeric(factor(df$patient_id)) %/% 49

# The plot you want to split into multiple pages
plot <- qplot(time, response, data = df, geom = "line", colour = treatment) +
facet_grid(~ patient_id, ncol = 7)

# For each group defined above, set the plot dataset to that subset
# and then print the resulting plot
d_ply(df, (.group), function(df) plot %+% df, .print = TRUE)

Regards,

Hadley

--
http://had.co.nz/

learnr

unread,
Mar 25, 2009, 5:50:50 AM3/25/09
to ggplot2
I would suggest using
df$group <- (as.numeric(factor(df$patient_id)) -1) %/% 49
to avoid getting 48 plots on the first page. See example below.

> 1:10 %/% 4
[1] 0 0 0 1 1 1 1 2 2 2
> (1:10 -1) %/% 4
[1] 0 0 0 0 1 1 1 1 2 2
Message has been deleted
Message has been deleted
Message has been deleted

hadley wickham

unread,
Mar 25, 2009, 8:44:32 AM3/25/09
to learnr, ggplot2
> I would suggest using
> df$group <- (as.numeric(factor(df$patient_id)) -1) %/% 49
> to avoid getting 48 plots on the first page. See example below.
>
>> 1:10 %/% 4
>  [1] 0 0 0 1 1 1 1 2 2 2
>> (1:10 -1) %/% 4
>  [1] 0 0 0 0 1 1 1 1 2 2

Good point - I always forget about that.

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages