Andy Bunn
2006-Mar-10 18:03 UTC
[R] add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...
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
Deepayan Sarkar
2006-Mar-10 21:10 UTC
[R] add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...
On 3/10/06, Andy Bunn <abunn at whrc.org> 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/
Possibly Parallel Threads
- test the significances of two regression lines
- Comparing two regression slopes
- Second y-axis in xyplot (lattice) where y1 and y2 have different ranges
- Comparing two regression lines
- as.formula and lme ( Fixed effects: Error in as.vector(x, "list") : cannot coerce to vector)