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

时间:2013-10-26 09:41:53

标签: r graphics lattice

我试图像这样制作一个格子条形图函数的包装器(默认情况下使用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,则有效:

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)

enter image description here

1 个答案:

答案 0 :(得分:2)

Barchart期待个人争论,而不是一个骑士。我会做这样的事情:

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

enter image description here