2013-10-26 41 views
4

我试图作出这样的格子条形图功能的包装(使默认使用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

回答

2

条形图期待个别论点,而不是一个pairlist。我会做这样的事情:

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

enter image description here

+0

什么是个人的论点和成对列表之间的区别是什么? – qed

+0

'paired.f < - function(list(x = 1))doSomeTasks'和'inv.f < - function(x = 1)doSomeTasks'。 – agstudy

+0

所以'...'是一个配对列表,'do.call(func,...)'使用列表中的单个参数? – qed