为什么这个格子条形图包装不起作用?

我试图像这样制作一个格子条形图函数的包装器(默认情况下使用ggplot主题):

require(ggplot2)
require(lattice)
require(latticeExtra)
data(Titanic)
mytheme = ggplot2like()
gbarchart = function(...) {
    barchart(..., par.settings=mytheme)
}
gbarchart(Class ~ Freq | Sex + Age,
          as.data.frame(Titanic),
          groups = Survived,
          stack = TRUE,
          layout = c(4, 1),
          auto.key = list(title = "Survived", columns = 2),
          scales = list(x = "free"))

它给出了一个错误:

Error in eval(expr, envir, enclos) : 
  ..3 used in an incorrect context, no ... to look in

如果par.settings = mytheme直接添加到条形图,它可以工作:

barchart(Class ~ Freq | Sex + Age,
          as.data.frame(Titanic),
          groups = Survived,
          stack = TRUE,
          layout = c(4, 1),
          auto.key = list(title = "Survived", columns = 2),
          scales = list(x = "free"),
          par.settings=mytheme)

最佳答案 Barchart期待个人争论,而不是骑士.我会做这样的事情:

gbarchart = function(...) {
  args <- as.list(match.call()[-1])
  args$par.settings=mytheme
  do.call(barchart,args)
}
点赞