I do not understand how to handle objects, stored in a data.frame, in certain lattice plots. In the second plot I get the error msg bellow. Is it possible to get it to work?
require(lattice)
require(latticeExtra)
data<-data.frame(a=I(list(1,2,3)),b=factor(1:3))
ecdfplot(~a|b,data=data
,layout=c(1,3)
,panel=function(x,...){
print(x[[1]])
panel.xyplot(x[[1]],.5,col=2)
}
)
data<-data.frame(a=I(list(diag(1,2,2),diag(1,2,2),diag(1,2,2))),b=factor(1:3))
ecdfplot(~a|b,data=data
,layout=c(1,3)
,panel=function(x,...){
print(x[[1]][1,1])
panel.xyplot(x[[1]][1,1],.5,col=2)
}
)
Error in prepanel.default.function(darg = list(give.Rkern = FALSE, n = 50, :
(list) object cannot be coerced to type 'double'
From
?ecdfplot:If
x(in your case the variablea) is not numeric, it gets coerced viaas.numericat many points along the road to getting plotted. One of those points is in theprepanelfunction.In your first
dataobject,data$acan be coerced without an error to a numeric (double) vector. In your seconddataobject,data$acannot be successfully coerced.Even though you specify a
panelfunction that should work, theprepanelfunction will throw an error.UPDATE after clarification:
There is a way to pass arbitrary objects to the panel function, and this is done by passing an argument via the ellipses of
ecdfplot. The argument must be named in a way that it does not match any named argument in the normal lattice function (and it’s best to avoid any named argument in the panel functions used). Furthermore, the formula argument toxinecdfplotmust also represent data that theecdfplotfunction can handle, i.e., not a list, as explained above.In the example below, I pass the data.frame
data4twice to the plot function: once as the argumentdataand once via the ellipses as the argumentplotData. This will pass the whole data.frame to the panel function, and it is thus necessary to pass thesubscriptsto the panel function as well, so that the appropriate data can be subscripted.You’ll notice from the plot that the x-axis limits cover the range of
aand extend beyond the range of the plotted values. Of course, one could correct this by using a custom prepanel function.