[R] Matrix as input to xyplot {lattice} - proper extended formula syntax

Deepayan Sarkar deepayan.sarkar at gmail.com
Sun Sep 6 09:11:53 CEST 2009


On Sat, Sep 5, 2009 at 10:43 PM, Bryan Hanson<hanson at depauw.edu> wrote:
> Thanks David, your way of constructing df is much more compact than what I
> was using, so I've incorporated it.  I also had my rows and columns
> transposed relative to how xyplot wanted them (though I had tested for that,
> other problems interfered).
>
> In my case, I may have varying numbers of y columns, from y.1 to y.n let's
> say.  Is there an easy way of creating the phrase y.1+y.2+...y.n to pass to
> xyplot, or even better, some sort of syntax that says "take all y.n" and
> plot them against x?

lm() etc does have such syntax, but not lattice unfortunately.
However, it's fairly easy (though inelegant) to create a formula from
a character string; e.g.

> as.formula(sprintf("%s ~ x", paste("y.", 1:5, sep = "", collapse = "+")))
y.1 + y.2 + y.3 + y.4 + y.5 ~ x

-Deepayan

>
> Thanks, Bryan
>
>
> On 9/6/09 12:51 AM, "David Winsemius" <dwinsemius at comcast.net> wrote:
>
>> I'm not exactly sure what structure df has. Here's my effort to
>> duplicate it:
>>
>> df <- data.frame(y=matrix(rnorm(24), nrow=6), x=1:6)
>>> df
>>           y.1        y.2        y.3        y.4 x
>> 1  0.1734636  0.2348417 -1.2375648 -1.3246439 1
>> 2  1.9551669 -1.1027262 -0.7307332  0.3953752 2
>> 3 -0.7645778  1.6297861  0.4743805 -0.4476145 3
>> 4 -0.5308756 -0.5246534 -0.3854609 -1.6097777 4
>> 5  0.7406525 -0.8691720 -0.8194084  1.6122059 5
>> 6 -0.9625619 -1.0774165  1.0760829  0.3659436 6
>>
>> And this seems to accomplish the desired task. Presumably you have
>> assigned off-stage the value of title to a meaningful character string?
>>
>>> p <- xyplot(y.1+y.2+y.3+y.4 ~ x |1:4, data = df, main =
>> "title" ,layout=c(1,4) )
>>> p
>>
>>
>>
>>
>> On Sep 5, 2009, at 11:52 PM, Bryan Hanson wrote:
>>
>>> Hello R Folks...
>>>
>>> I have a list with the following structure:
>>>
>>>> str(df)
>>> List of 3
>>> $ y    : num [1:4, 1:1242] -0.005379 0.029874 -0.023274 0.000655
>>> -0.004537
>>> ..
>>> $ x    : num [1:1242] 501 503 505 507 509 ...
>>> $ names: Factor w/ 4 levels "PC Loading 1",..: 1 2 3 4
>>>
>>> I want to plot each row of df$y against df$x, and have each plot in
>>> it¹s own
>>> panel according to the levels of df$names.  The following works in
>>> the sense
>>> that the layout is right, but the y values have clearly been
>>> recycled or
>>> skipped in some fashion (and an error is thrown for each panel that
>>> the
>>> length of x and y aren¹t the same):
>>>
>>> p <- xyplot(y ~ x | names, data = df, main = title,
>>>        layout = c(1, dim(y)[1])
>>>
>>> In reviewing the extended formula interface in the Lattice Book,
>>> what I want
>>> to happen is y1 + y2 + y3 + y4 ~ x | names, outer = TRUE
>>>
>>> I see two options: figure out a way to create the extended formula
>>> on the
>>> fly (and the actual number of rows in y may vary), which seems
>>> potentially
>>> tricky, or create a data frame by stacking each row of y and
>>> repeating x and
>>> names to match.  This seems like a waste of memory.
>>>
>>> I¹ve looked through the archives and haven¹t come across something
>>> quite
>>> like this, or at least I don¹t recognize it if I have!  Is there a
>>> more
>>> elegant way to tell xyplot I want to use each row of y repeatedly
>>> with the
>>> same x, in a loop-like fashion?
>>>
>>> TIA.  Bryan
>>> *************
>>> Bryan Hanson
>>> Professor of Chemistry & Biochemistry
>>> DePauw University, Greencastle IN USA
>>>
>>>
>>> [[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.
>>
>> David Winsemius, MD
>> Heritage Laboratories
>> West Hartford, CT
>>
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list