[R] Fit lines in intxplot

Peter Ehlers ehlers at ucalgary.ca
Fri Jun 1 13:32:35 CEST 2012


On 2012-05-31 21:41, arun wrote:
> Dear R help,
>
> I tried to add linear fit lines in intxplot by adding "index.cond = function(x,y) coef(lm(y ~ x))[1]" inside the intxplot().  It didn't help.  I would appreciate any help.
> My dataset and codes are pasted below.
>
> Thanks,
> A.K.
>
>
> datGreen<- read.table(text="
> Time,    DarkSt,    LightSt
> 0,    0,    1
> 1,    0.03,    0.82
> 2,    0.06,    0.91
> 3,    0.12,    0.64
> 4,    0.12,    0.73
> 5,    0.12,    0.64
> 6,    0.09,    0.64
> 7,    0.09,    0.64
> 8,    0.09,    0.64
> 9,    0.09,    0.64
> 10,    0.12,    0.64
> 11,    0.09,    0.64
> 12,    0.09,    0.73
> 13,    0.06,    0.73
> 14,    0.06,    0.64
> 15,    0.09,    0.73
> 16,    0.09,    0.73
> 17,    0.09,    0.73
> 18,    0.12,    0.64
> 19,    0.09,    0.64
> 20,    0.12,    0.64
> ",sep=",",header=TRUE)
>
> dat1<-data.frame(time=rep(c(1:20),rep(2,20)),trt=rep(c('D','L'),each=1))
>      datG<-data.frame(matrix(rbind(datGreen$DarkSt,datGreen$LightSt),ncol=1,byrow=TRUE))
>      colnames(datG)<-"Response"
>      dat2G<-datG[-1,]
>      dat2G<-data.frame(dat2G)
>      dat2G<-dat2G[-1,]
>      dat2G<-data.frame(dat2G)
>      colnames(dat2G)<- "Response"
>      dat3G<-data.frame(dat1,dat2G)
>      dat4G<-transform(dat3G,trt=ordered(trt,levels=c('D','L')))
>       dat4G$time<-factor(dat4G$time)
>      position(dat4G$time)<-as.numeric(levels(dat4G$time))
>      position(dat4G$trt)<-c(10,20)
> #pdf("GreenDarkvsLight.pdf")
> intxplot(Response~time,data=dat4G,groups=trt,se=TRUE,ylim=c(0,1),main="Response in Green light")
>
> #dev.off()
>
> I expect to get fit lined as in the xyplot:
> xyplot(Response ~ time, dat4G,
>                type = c( "g", "p","r"), groups=trt, index.cond = function(x,y) coef(lm(y ~ x))[1],
>               xlab = "Time in min",
>               ylab = "Response", main="Response in Green light")

Is this what you have in mind:

library(HH)
  intxplot(Response ~ time, data = dat4G, groups = trt,
    panel = function(x, y, subscripts, groups, ...){
      panel.intxplot(x, y, subscripts, groups)
      panel.superpose(x, y, ..., panel.groups = panel.lmline,
                     groups = groups, subscripts = subscripts)
    })

See the bwplot example in ?panel.lmline.

Notice that I've stripped the unnecessary titles, etc. in order to
produce a less bloated example. I've alse made it clear that the
intxplot() function is in the HH package (which you should have said).

Peter Ehlers
--



More information about the R-help mailing list