2013-10-26 69 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