#Dear All, 
#I'm having a bit of a trouble here, please help me...
#I have this data
set.seed(4)
mydata <- data.frame(var = rnorm(100),
                     temp = rnorm(100),
                     subj = as.factor(rep(c(1:10),5)),
                     trt = rep(c("A","B"), 50))

#and this model that fits them
lm  <- lm(var ~ temp * subj, data = mydata)

#i want to plot the results with lattice anf fit the regression line, predicted with my model, trough them
#to do so, I'm using this approach, outlined  "Lattice Tricks for the power useR" by D. Sarkar

temp_rng <- range(mydata$temp, finite = TRUE)

grid <- expand.grid(temp = do.breaks(temp_rng, 30),
                    subj = unique(mydata$subj),
                    trt = unique(mydata$trt))

model <- cbind(grid, var = predict(lm, newdata = grid))

orig <- mydata[c("var","temp","subj","trt")]

combined <- make.groups(original = orig, model = model)


xyplot(var ~ temp | subj, 
       data = combined,
       groups = which,
       type = c("p", "l"),
       distribute.type = TRUE
       )


# so far every thing is fine, but, i also whant assign a filling to the data points for the two treatments trt=1 and trt=2 
# so I have written this piece of code, that works fine, but when it comes to plot the regression line, it seems that type is not recognized by the panel function... 

my.fill <- c("black", "grey")

plot <- with(combined,
        xyplot(var ~ temp | subj,
              data = combined,
              group = combined$which,
              type = c("p", "l"),
              distribute.type = TRUE,
              panel = function(x, y, ..., subscripts){
           	         fill <- my.fill[combined$trt[subscripts]] 
           	         panel.xyplot(x, y, pch = 21, fill = my.fill, col = "black")
           	         },
             key = list(space = "right",
                     text = list(c("trt1", "trt2"), cex = 0.8),
                     points = list(pch = c(21), fill = c("black", "grey")),
                     rep = FALSE)
                     )
      )
plot

#I've also tried to move type and distribute type within panel.xyplot, as well as subsseting the data in it panel.xyplot like this

plot <- with(combined,
        xyplot(var ~ temp | subj,
              data = combined,
              panel = function(x, y, ..., subscripts){
           	         fill <- my.fill[combined$trt[subscripts]] 
           	         panel.xyplot(x[combined$which=="original"], y[combined$which=="original"], pch = 21, fill = my.fill, col = "black")
           	         panel.xyplot(x[combined$which=="model"], y[combined$which=="model"], type = "l", col = "black")
           	         },
             key = list(space = "right",
                     text = list(c("trt1", "trt2"), cex = 0.8),
                     points = list(pch = c(21), fill = c("black", "grey")),
                     rep = FALSE)
                     )
      )
plot

#but no success with that either...
#can anyone help me to get the predicted values plotted as a line instead of being points? 
#really appricieate
#matteo 





	[[alternative HTML version deleted]]

