[R] two lattice graphs in one object

Felix Andrews felix at nfrac.org
Wed Sep 3 10:22:58 CEST 2008


By the way, a different approach, with a different result, is to use
c.trellis from the latticeExtra package:

library(latticeExtra)
...
return(c(gr1, gr2))

This merges the panels into a single trellis object. Note that there
are complications with things like titles, legends and strips.

-Felix

2008/9/3 Andreas Krause <andreas at elmo.ch>:
> Deepayan,
>
> that is exactly what I was hoping for. Thanks much!
>
> Experimenting with it I noticed that updating two existing objects with plot arguments seems to not work, at least not in this way:
>
> gr1 <-xyplot(rnorm(111) ~ runif(111), main = "Plot A")
> gr2 <- xyplot(runif(111) ~ runif(111), main = "Plot B")
>
> plist <- list(
>  update(gr1, plot.args = list(position = c(0, 0, 1, 0.5), more = TRUE)),
>  update(gr2, plot.args = list(position = c(0, 0.5, 1, 1), more = FALSE))
> )
>
> print(plist)
> gives me two separate pages, even though the object is updated:
>
>> plist[[1]]$panel.args.common
> $plot.args
> $plot.args$position
> [1] 0.0 0.0 1.0 0.5
>
> $plot.args$more
> [1] TRUE
>
> I can get it to work the "dirty" way:
> plist[[1]]$plot.args <-  list(position = c(0, 0, 1, 0.5), more = TRUE)
> plist[[2]]$plot.args <-  list(position = c(0, 0.5, 1, 1), more = FALSE)
>
> Shouldn't update work as well?
> (R version 2.6.2, platform i386-pc-mingw32)
>
> Thanks again!
>
>  Andreas
>
>  On Tue Sep  2 21:43 , "Deepayan Sarkar"  sent:
>
>>On Tue, Sep 2, 2008 at 6:24 AM, Andreas Krause andreas at elmo.ch> wrote:
>>>
>>> When I create a lattice/Trellis type graph, I typically write a function that returns the graph, as in
>>> do.graph
>>>  {
>>>    require(lattice)
>>>    return(xyplot(y~x, ...))
>>>  }
>>>
>>> My question today is this:
>>> If I want two graphs on one page, one way of achieving it is to print the objects into defined areas, as in
>>>
>>> gr1
>>> gr2
>>> print(gr1, pos=c(0, 0, 1, 0.5), more=T)
>>> print(gr2, pos=c(0, 0.5, 1, 1), more=F)
>>>
>>> Instead of using the print method, can I create a single trellis object that contains those two "sub-graphs"?
>>> I do not think so, given what I know about the design of these objects.
>>> I am hoping for a pleasant surprise though.
>>
>>Well, you cannot return it as a single "trellis" object. However, you
>>could always return it as a list with multiple "trellis" objects, and
>>they will just get printed one by one. You can attach print()
>>arguments to the objects themselves, so that takes care of the layout.
>>For example, try
>>
>>plist
>>    gr1
>>        xyplot(rnorm(111) ~ runif(111), main = "Plot A",
>>               plot.args = list(position = c(0, 0, 1, 0.5), more = TRUE))
>>    gr2
>>        xyplot(runif(111) ~ runif(111), main = "Plot B",
>>               plot.args = list(position = c(0, 0.5, 1, 1), more = FALSE))
>>    list(gr1, gr2)
>>}
>>
>>print(plist)
>>
>>
>>Actually you will see some output on the console:
>>
>>> print(plist)
>>[[1]]
>>
>>[[2]]
>>
>>This is from the print method for lists. If you want to avoid that,
>>you can always set a class on the list you return and write a
>>corresponding print() method.
>>
>>-Deepayan
>
> ______________________________________________
> 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