[R] Multiple plots on multiple devices

Felix Andrews felix at nfrac.org
Tue Nov 4 07:33:26 CET 2008


Hi Oliver,

These are my suggestions...

Option 1: use the playwith package (a GTK+ interface);

Option 2: use the latticist package (a gWidgets interface, will be
released in the next day or two);

Option 3: use this code:


plotOnePage <- function(x, page, ...)
{
    stopifnot(inherits(x, "trellis"))
    n <- page
    if (is.null(x$layout)) {
        if (length(dim(x)) > 2)
            x$layout <- dim(x)[1:2]
    }
    if (!is.null(x$layout))
        x$layout[3] <- 1
    ## based on code by Deepayan Sarkar
    packet.panel.pageN <- function(..., page)
        packet.panel.default(..., page = page + n - 1)
    plot(x, packet.panel = packet.panel.pageN, ...)
}

npages <- function(x)
{
    stopifnot(inherits(x, "trellis"))
    ## work out number of pages that would be plotted
    ## to display trellis object 'x'
    nPackets <- prod(dim(x))
    ## by default, first two dimensions
    ## (conditioning variables) shown on each page
    nPanels <- prod(head(dim(x), 2))
    ## but if an explicit 'layout' is given...
    if (!is.null(x$layout)) {
        nPanels <- x$layout[1] * x$layout[2]
        if (x$layout[1] == 0) nPanels <- x$layout[2]
    }
    ## TODO: what about 'skip'?
    nPages <- ceiling(nPackets / nPanels)
    nPages
}

plotEachPage <- function(x, ...)
{
    for (i in seq(length = npages(x))) {
        dev.new(...)
        plotOnePage(x, i)
    }
}

foo <- barchart(Titanic, groups = FALSE)
npages(foo)
# 2
plotEachPage(foo)

baz <- qqmath(~ state.x77[,"Income"] | state.region,
    layout = c(1,1))
npages(baz)
# 4
plotEachPage(baz)


2008/11/3 Oliver Kimberlin <oliver at mango-solutions.com>:
> Hi List,
>
> This is possibly a newbie error. I have however searched long and hard
> and haven't found a solution.
>
> I am attempting to plot multiple lattice graphs in R, plotting 4 per
> page and moving on to a new device to plot the next 4 and so on. (I want
> to do this in R and not export to a pdf etc)
>
> I notice that in S-PLUS multiple tabs are opened in one device, R does
> not seem to have this capability and as I plot the next 4 graphs they
> overwrite the previous 4.
>
> I imagine a 'for' loop could solve this by opening a new active device
> at the end of the loop, but I feel this is inefficient and that there
> must be a solution by modifying the lattice object.
>
> Let z be a lattice object. (histogram for example)
>
> Let the layout be.
>
> layout=cbind(c(1,2),c(3,4))
>
>     [,1] [,2]
>
> [1,]    1    3
>
> [2,]    2    4
>
> Print(z)
>
> Thus giving 4 graphs per page on multiple pages until the lattice object
> has been completely plotted.
>
> I hope this is clear
>
> Thanks all
>
> Oli
>
>
>
>
>
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Felix Andrews / 安福立
http://www.neurofractal.org/felix/
3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8



More information about the R-help mailing list