When we call a lattice function such as xyplot, to what extent does the "data" designation cause the function to look inside the "data" for variables? In the examples below, the "subset" argument understands that "Variety" is a variable in the data. But the "scales" argument does not understand that "nitro" is a variable in the data. What principle is at work? library(MEMSS) # The following works fine: xyplot( yield ~ nitro , data=Oats , scales=list( x=list( at=unique(Oats$nitro) ) ) , subset=Variety=="Victory" ) # But the following returns an error: xyplot( yield ~ nitro , data=Oats , scales=list( x=list( at=unique(nitro) ) ) ) Thanks for any insight Jacob A. Wegelin Assistant Professor Department of Biostatistics Virginia Commonwealth University 730 East Broad Street Room 3006 P. O. Box 980032 Richmond VA 23298-0032 U.S.A. E-mail: jwegelin at vcu.edu URL: http://www.people.vcu.edu/~jwegelin
Deepayan will correct me if I'm wrong, but I'm pretty sure that the answer is that it looks in the frame in the data argument only for variables in the formula argument. Note that the fact that it also works for the subset argument is explicitly mentioned therein: subset: logical or integer indexing vector **(can be specified in terms of variables in data). ** FWIW, I agree that this is not as clearly documented as I think it could be: I kind of intuited it after a bunch of trial and error. Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jacob Wegelin Sent: Monday, October 12, 2009 9:33 AM To: r-help at stat.math.ethz.ch Subject: [R] xyplot does not find variable in data When we call a lattice function such as xyplot, to what extent does the "data" designation cause the function to look inside the "data" for variables? In the examples below, the "subset" argument understands that "Variety" is a variable in the data. But the "scales" argument does not understand that "nitro" is a variable in the data. What principle is at work? library(MEMSS) # The following works fine: xyplot( yield ~ nitro , data=Oats , scales=list( x=list( at=unique(Oats$nitro) ) ) , subset=Variety=="Victory" ) # But the following returns an error: xyplot( yield ~ nitro , data=Oats , scales=list( x=list( at=unique(nitro) ) ) ) Thanks for any insight Jacob A. Wegelin Assistant Professor Department of Biostatistics Virginia Commonwealth University 730 East Broad Street Room 3006 P. O. Box 980032 Richmond VA 23298-0032 U.S.A. E-mail: jwegelin at vcu.edu URL: http://www.people.vcu.edu/~jwegelin ______________________________________________ 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.
On Mon, Oct 12, 2009 at 9:32 AM, Jacob Wegelin <jacob.wegelin at gmail.com> wrote:> When we call a lattice function such as xyplot, to what extent does > the "data" designation cause the function to look inside the "data" > for variables? > > In the examples below, the "subset" argument understands that > "Variety" is a variable in the data. > > But the "scales" argument does not understand that "nitro" is a > variable in the data. > > What principle is at work?A strange one called "standard non-standard evaluation"; see http://developer.r-project.org/nonstandard-eval.pdf for a nice overview by Thomas Lumley. ?xyplot says: data: For the ?formula? method, a data frame containing values (or more precisely, anything that is a valid ?envir? argument in ?eval?, e.g. a list or an environment) for any variables in the formula, as well as ?groups? and ?subset? if applicable. If not found in ?data?, or if ?data? is unspecified, the variables are looked for in the environment of the formula. For other methods (where ?x? is not a formula), ?data? is usually ignored, often with a warning. so the non-standard evaluation only applies to 'groups' and 'subset'. The list may be different for other functions, e.g., densityplot() also evaluates 'weights' in 'data'. -Deepayan
One other way that is sometimes useful is to use the tick.number argument as in: xyplot(yield ~ nitro, data=Oats, scales=list(x=list(tick.number=4)), subset=Variety=="Victory" ) This is especially handy if you want to just tick/label every other value. -Peter Ehlers Jacob Wegelin wrote:> When we call a lattice function such as xyplot, to what extent does > the "data" designation cause the function to look inside the "data" > for variables? > > In the examples below, the "subset" argument understands that > "Variety" is a variable in the data. > > But the "scales" argument does not understand that "nitro" is a > variable in the data. > > What principle is at work? > > > library(MEMSS) > > # The following works fine: > > xyplot( > yield ~ nitro > , data=Oats > , scales=list( > x=list( > at=unique(Oats$nitro) > ) > ) > , subset=Variety=="Victory" > ) > > # But the following returns an error: > > xyplot( > yield ~ nitro > , data=Oats > , scales=list( > x=list( > at=unique(nitro) > ) > ) > ) > > Thanks for any insight > > Jacob A. Wegelin > Assistant Professor > Department of Biostatistics > Virginia Commonwealth University > 730 East Broad Street Room 3006 > P. O. Box 980032 > Richmond VA 23298-0032 > U.S.A. > E-mail: jwegelin at vcu.edu > URL: http://www.people.vcu.edu/~jwegelin > > ______________________________________________ > 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. > >