The problem appears to be that y
loses its dimension attribute when it's passed into the panel function, becoming a simple vector. It still goes ahead and plots, recycling x
to match y
's length, which you can't see type="p"
, but can when type="l"
.
Here is a custom panel function that accomplishes what you want by first converting y
back to a matrix and then calling panel.xyplot
separately on each of its columns:
panel.matplot <- function(x,y,...) {
y <- matrix(y, nrow=length(x))
apply(y, 2, function(Y) panel.xyplot(x,Y, ...))
}
xyplot(pred~Age|Sex, data=pred.Alb, type="l", panel=panel.matplot)
BTW: In cases like this, I often find it useful to poke around 'inside' the panel function call. A simple way to do this is to construct a dummy panel function containing a browser()
call. Here, for example, is how I discovered the problem in this case:
xyplot(pred~Age|Sex, data=pred.Alb, type="l",
panel = function(x,y,...) browser())
Browse[2]> x
# [1] 20 30 40 50 60 70 80
Browse[2]> y
# [1] 4.349769 4.316590 4.283410 4.239521 4.152793 4.055355 3.957917 4.377092
# [9] 4.389149 4.401206 4.396423 4.324283 4.235303 4.146323 4.597817 4.599101
# [17] 4.600385 4.581327 4.480899 4.360128 4.239358
... at which point the required fix is both (a) pretty obvious and (b) can be tested out from within the existing browser call.