add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...

Andy Bunn
Although this should be trivial, I'm having a spot of trouble.

I want to make a lattice plot of the format y1+y2 ~ x | grp but then fit a
lm to each y variable and add an abline of those models in different colors.
If the xyplot followed y~x|grp I would write a panel function as below, but
I'm unsure of how to do that with y1 and y2 without reshaping the data
before hand. Thoughts appreciated. -Andy



foo <- data.frame(y1 = 1:25+rnorm(100, -3, 1), y2 = 1:25+rnorm(100,3,1), x =
rep(1:25,4), grp = rep(letters[1:4],25))
# I want to add a trend line for y1 and y2 here:
xyplot(y1+y2 ~ x | grp, data = foo)
# like this example for just one y variable:
xyplot(y1~x|grp, data = foo, panel = function(x,y)
          { lm1 = lm(y~x)
            panel.points(x,y, col = "red")
            panel.abline(lm1, col = "red")
            #lm2 = lm(y~x) # model for y2
            #panel.points(x,y, col = "blue") #points for y2
            #panel.abline(lm2, col = "blue") #abline for y2
          })



> version
         _
platform i386-pc-mingw32
arch     i386
os       mingw32
system   i386, mingw32
status
major    2
minor    2.1
year     2005
month    12
day      20
svn rev  36812
language R

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...

Deepayan Sarkar
On 3/10/06, Andy Bunn <[hidden email]> wrote:

> Although this should be trivial, I'm having a spot of trouble.
>
> I want to make a lattice plot of the format y1+y2 ~ x | grp but then fit a
> lm to each y variable and add an abline of those models in different colors.
> If the xyplot followed y~x|grp I would write a panel function as below, but
> I'm unsure of how to do that with y1 and y2 without reshaping the data
> before hand. Thoughts appreciated. -Andy
>
>
>
> foo <- data.frame(y1 = 1:25+rnorm(100, -3, 1), y2 = 1:25+rnorm(100,3,1), x =
> rep(1:25,4), grp = rep(letters[1:4],25))
> # I want to add a trend line for y1 and y2 here:
> xyplot(y1+y2 ~ x | grp, data = foo)
> # like this example for just one y variable:
> xyplot(y1~x|grp, data = foo, panel = function(x,y)
>           { lm1 = lm(y~x)
>             panel.points(x,y, col = "red")
>             panel.abline(lm1, col = "red")
>             #lm2 = lm(y~x) # model for y2
>             #panel.points(x,y, col = "blue") #points for y2
>             #panel.abline(lm2, col = "blue") #abline for y2
>           })

Depending on at what level your question approximates your real question,

xyplot(y1+y2~x|grp, data = foo, type = c('r', 'p'))

or

xyplot(y1+y2~x|grp, data = foo, panel = panel.superpose,
           panel.groups = function(x,y,...)
          { lm1 = lm(y~x)
            panel.points(x,y, col = "red")
            panel.abline(lm1, col = "red")
            #lm2 = lm(y~x) # model for y2
            #panel.points(x,y, col = "blue") #points for y2
            #panel.abline(lm2, col = "blue") #abline for y2
          })

(the body of the function is unchanged, but the argument list has a
..., which is important).

Deepayan
--
http://www.stat.wisc.edu/~deepayan/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...

Andy Bunn
> -----Original Message-----
> From: Deepayan Sarkar [mailto:[hidden email]]
> Sent: Friday, March 10, 2006 4:10 PM
> To: Andy Bunn
> Cc: R-Help
> Subject: Re: add trend line to each group of data in: xyplot(y1+y2 ~ x |
> grp...
>
>
> On 3/10/06, Andy Bunn <[hidden email]> wrote:
> > Although this should be trivial, I'm having a spot of trouble.
> >
> > I want to make a lattice plot of the format y1+y2 ~ x | grp but
> then fit a
> > lm to each y variable and add an abline of those models in
> different colors.
> > If the xyplot followed y~x|grp I would write a panel function
> as below, but
> > I'm unsure of how to do that with y1 and y2 without reshaping the data
> > before hand. Thoughts appreciated. -Andy
> >
> >
> >
> > foo <- data.frame(y1 = 1:25+rnorm(100, -3, 1), y2 =
> 1:25+rnorm(100,3,1), x =
> > rep(1:25,4), grp = rep(letters[1:4],25))
> > # I want to add a trend line for y1 and y2 here:
> > xyplot(y1+y2 ~ x | grp, data = foo)
> > # like this example for just one y variable:
> > xyplot(y1~x|grp, data = foo, panel = function(x,y)
> >           { lm1 = lm(y~x)
> >             panel.points(x,y, col = "red")
> >             panel.abline(lm1, col = "red")
> >             #lm2 = lm(y~x) # model for y2
> >             #panel.points(x,y, col = "blue") #points for y2
> >             #panel.abline(lm2, col = "blue") #abline for y2
> >           })
>
> Depending on at what level your question approximates your real question,
>
> xyplot(y1+y2~x|grp, data = foo, type = c('r', 'p'))
>
> or
>
> xyplot(y1+y2~x|grp, data = foo, panel = panel.superpose,
>            panel.groups = function(x,y,...)
>           { lm1 = lm(y~x)
>             panel.points(x,y, col = "red")
>             panel.abline(lm1, col = "red")
>             #lm2 = lm(y~x) # model for y2
>             #panel.points(x,y, col = "blue") #points for y2
>             #panel.abline(lm2, col = "blue") #abline for y2
>           })
>
> (the body of the function is unchanged, but the argument list has a
> ..., which is important).
>
> Deepayan
> --
> http://www.stat.wisc.edu/~deepayan/

Deepayan:

I'm really trying to understand how to access the different groups in this type of plot. Imagine I have the same scenario as above, an xyplot of the form y1+y2 ~ x | grp. Now, how can I modify the call to panel.text to write, say, the correlation coeff from each model to different portions of each panel (the upper left corner for the correlation of (x,y2) and the lower right corner for x,y2). The code below, if uncommented, executes but obviously doesn't do what I want it to do. Thoughts appreciated and gratitude as always. -Andy


foo <- data.frame(y1 = 1:25+rnorm(100, -3, 1), y2 = 1:25+rnorm(100,3,1), x = rep(1:25,4), grp = rep(letters[1:4],25))
y.range <- range(foo$y1, foo$y2)
x.range <- range(foo$x)
xyplot(y1+y2~x|grp, data = foo, panel = panel.superpose,
       col.line=c('red','blue'), col.symbol=c('red','blue'), panel.groups = function(x,y,col.line,col.symbol,...)
          { panel.points(x,y,col=col.symbol)
            panel.lmline(x,y,col=col.line)
#            # if the data being plotted are from y1 then
#            # write the cor coef in the lower right corner
#            aCor=round(cor(x,y),digits=2)
#            panel.text(x.range[2],y.range[1],paste("r=,",aCor),pos=2,col="red")
#            # if the data being plotted are from y2 then
#            # write the cor coef in the upper left corner
#            aCor=round(cor(x,y),digits=2)
#            panel.text(x.range[1],y.range[2],paste("r=,",aCor),pos=4,col="blue")
          })

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...

Deepayan Sarkar
On 3/13/06, Andy Bunn <[hidden email]> wrote:

>
> Deepayan:
>
> I'm really trying to understand how to access the different groups in this
> type of plot. Imagine I have the same scenario as above, an xyplot of the
> form y1+y2 ~ x | grp. Now, how can I modify the call to panel.text to write,
> say, the correlation coeff from each model to different portions of each
> panel (the upper left corner for the correlation of (x,y2) and the lower
> right corner for x,y2). The code below, if uncommented, executes but
> obviously doesn't do what I want it to do. Thoughts appreciated and
> gratitude as always. -Andy
>
>
> foo <- data.frame(y1 = 1:25+rnorm(100, -3, 1), y2 = 1:25+rnorm(100,3,1), x =
> rep(1:25,4), grp = rep(letters[1:4],25))
> y.range <- range(foo$y1, foo$y2)
> x.range <- range(foo$x)
> xyplot(y1+y2~x|grp, data = foo, panel = panel.superpose,
>        col.line=c('red','blue'), col.symbol=c('red','blue'), panel.groups =
> function(x,y,col.line,col.symbol,...)
>           { panel.points(x,y,col=col.symbol)
>             panel.lmline(x,y,col=col.line)
> #            # if the data being plotted are from y1 then
> #            # write the cor coef in the lower right corner
> #            aCor=round(cor(x,y),digits=2)
> #
> panel.text(x.range[2],y.range[1],paste("r=,",aCor),pos=2,col="red")
> #            # if the data being plotted are from y2 then
> #            # write the cor coef in the upper left corner
> #            aCor=round(cor(x,y),digits=2)
> #
> panel.text(x.range[1],y.range[2],paste("r=,",aCor),pos=4,col="blue")
>           })

There was a very similar question off-list a few days back. My response was:

``
There's no good way I can think of. 'panel.groups' simply does not
have any information about which level of 'groups' is being used. I
think it should, and I've changed the behaviour so that from R 2.3.0
'panel.groups' will be called with an argument called 'group.number'
which will specify the level. There are many ways to work around this
for now (e.g. write your own panel function that eventualy calls
panel.superpose). The simplest is to use a global counter variable;
increment it inside panel.groups using <<- (needless to say, this is
not good programming practice in general).
''

Deepayan

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html