[R] 2 Trellis graphics question

2007-06-12 Thread Alan S Barnett
1) I have a data that includes some bad data.  I want to make a
trellis plot where each panel contains 
a) A scatter plot of the good data
b) A scatter plot of the bad data in a different color
c) A best fit line of all the data, and
d) A best fit line of the good data.

I tried using xyplot and setting the group argument, but I'm having
trouble.  Here is my code:

xyplot(y ~ x | status, data=data,groups=good,
+  panel=function(x,y,...){
+  panel.xyplot(x,y,...)
+  panel.lmline(x,y,col = red)
+  panel.lmline(x[good],y[good],col = blue)
+ } 
+ )
a,groups=good,
data is a data frame containing names x, y and status.
good is logical variable labeling the good data.
Each panel of the trellis plot generated by this code includes
a) A scatterplot of the good data in magenta,
b) A scatterplot of the bad data in blue,
c) A best fit line of all the data in blue.

If I reverse the order of the last two lines, the best fit lines are
displayed in red.  Clearly, the correct value of good is not being
passed to panel.lmline, but is being correctly passed to panel.xyplot.

Substituting 
 panel.lmline(x,y,...)
for
 panel.lmline(x,y,col = red) 
 panel.lmline(x[good],y[good],col = blue)
results in the best fit line being plotted in black.
 
2) There are 5 different values of status, but I only want to plot three
of them.  Can I do this without copying only the desired elements into a
new data frame?

-- 
Alan S Barnett [EMAIL PROTECTED]
NIMH/CBDB

__
R-help@stat.math.ethz.ch 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.


Re: [R] 2 Trellis graphics question

2007-06-12 Thread Deepayan Sarkar
On 6/12/07, Alan S Barnett [EMAIL PROTECTED] wrote:
 1) I have a data that includes some bad data.  I want to make a
 trellis plot where each panel contains
 a) A scatter plot of the good data
 b) A scatter plot of the bad data in a different color
 c) A best fit line of all the data, and
 d) A best fit line of the good data.

 I tried using xyplot and setting the group argument, but I'm having
 trouble.  Here is my code:

 xyplot(y ~ x | status, data=data,groups=good,
 +  panel=function(x,y,...){
 +  panel.xyplot(x,y,...)
 +  panel.lmline(x,y,col = red)
 +  panel.lmline(x[good],y[good],col = blue)
 + }
 + )

You are close, except the last panel.lmline call is not meaningful
because the 'x' and 'y' in the panel function are not the same length
as 'good'.  You need to use 'subscripts' for that.

To fix ideas, here's a concrete example (I've changed the names 'x'
and 'y' to 'xx' and 'yy' to avoid any confusion):


mydata -
data.frame(xx = sample(100),
   yy = rnorm(100) + rep(c(5, 10), c(80, 20)),
   status = gl(5, 1, 100),
   good = rep(c(TRUE, FALSE), c(80, 20)))

Then, what you want can be achieved with:

xyplot(yy ~ xx | status, mydata, groups = good,

   panel = function(x, y, groups, subscripts, ...) {
   panel.xyplot(x, y,
groups = groups,
subscripts = subscripts,
...)
   panel.lmline(x, y, col = red)
   good.id - groups[subscripts]
   ## good.id: subset of 'good' relevant for this panel
   panel.lmline(x[good.id], y[good.id], col = blue)
   })

This also works if 'good' is globally visible and not in 'mydata'.

[...]

 2) There are 5 different values of status, but I only want to plot three
 of them.  Can I do this without copying only the desired elements into a
 new data frame?

Sure, just use the additional argument

  subset = (status %in% c(1, 3, 5))

or whatever the appropriate subset is.

-Deepayan

__
R-help@stat.math.ethz.ch 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.