简体   繁体   English

任意对象作为晶格函数的参数

[英]Arbitrary objects as argument for lattice functions

I do not understand how to handle objects, stored in a data.frame, in certain lattice plots. 我不明白如何在某些格子图中处理存储在data.frame中的对象。 In the second plot I get the error msg bellow. 在第二个图中我得到错误消息msg。 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'

convert x to a numeric correct the problem x转换为数字可以解决问题

    x <- x[[1]]
    panel.xyplot(as.numeric(x),.5,col=2)

From ?ecdfplot : 来自?ecdfplot

For the "formula" method, x is a formula describing the form of conditioning plot, and has to be of the form ~x, where x is assumed to be a numeric vector. 对于“公式”方法,x是描述条件图形式的公式,并且必须是~x的形式,其中x被假定为数字向量。

If x (in your case the variable a ) is not numeric, it gets coerced via as.numeric at many points along the road to getting plotted. 如果x (在您的情况下变量a )不是数字,则它会在道路的许多点通过as.numeric强制进行绘制。 One of those points is in the prepanel function. 其中一点是在prepanel功能中。

In your first data object, data$a can be coerced without an error to a numeric (double) vector. 在您的第一个data对象中, data$a可以在没有错误的情况下被强制转换为数字(双)向量。 In your second data object, data$a cannot be successfully coerced. 在第二个data对象中,无法成功强制data$a

> as.numeric(data$a)
Error: (list) object cannot be coerced to type 'double'

Even though you specify a panel function that should work, the prepanel function will throw an error. 即使您指定了应该起作用的panel功能, prepanel功能也会抛出错误。


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 . 有一种方法可以将任意对象传递给面板函数,这可以通过ecdfplot的省略号传递参数来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 to x in ecdfplot must also represent data that the ecdfplot function can handle, ie, not a list, as explained above. 此外, ecdfplot x的公式参数还必须表示ecdfplot函数可以处理的数据,即不是列表,如上所述。

In the example below, I pass the data.frame data4 twice to the plot function: once as the argument data and once via the ellipses as the argument plotData . 在下面的示例中,我将data.frame data4两次传递给绘图函数:一次作为参数data ,一次通过省略号作为参数plotData This will pass the whole data.frame to the panel function, and it is thus necessary to pass the subscripts to the panel function as well, so that the appropriate data can be subscripted. 这会将整个data.frame传递给面板函数,因此有必要将subscripts传递给面板函数,以便可以下标相应的数据。

# New example data
data4 <- data.frame(a = 0:2, b = factor(1:3),
  forPrint = letters[1:3],
  forXyplot = I(list(list(x = seq(0, 1, .25), y = rev(seq(0, 1, .25))),
  list(x = seq(0, 1, .5), y = rev(seq(0, 1, .5))), list(x = seq(0, 1, .2),
  y = rev(seq(0, 1, .2))))))

> data4
  a b forPrint    forXyplot
1 0 1        a c(0, 0.2....
2 1 2        b c(0, 0.5....
3 2 3        c c(0, 0.2....

ecdfplot(~ a|b, data = data4
         ,layout=c(1,3)              
         ,panel=function(x, subscripts = subscripts, ...){
            # plotData is passed to the panel function via the ellipses,
            # so extract those arguments vial match.call
            args <- match.call(expand.dots = FALSE)$...
            # Print the column forPrint
            print(args$plotData$forPrint[subscripts])
            # Plot the forXyplot column using do.call, since the column is
            # a list with x and y items.
            do.call(panel.xyplot, c(args$plotData[subscripts, "forXyplot"][[1]],
              col=2))
          }
          ,plotData = data4
     )

You'll notice from the plot that the x-axis limits cover the range of a and extend beyond the range of the plotted values. 您将从图中注意到x轴限制覆盖了a的范围并超出了绘制值的范围。 Of course, one could correct this by using a custom prepanel function. 当然,可以通过使用自定义预付费功能来纠正此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM