【问题标题】:Why doesn't this lattice barchart wrapper work?为什么这个格子条形图包装器不起作用?
【发布时间】:2013-10-26 09:41:52
【问题描述】:

我正在尝试像这样对格子条形图函数进行包装(默认使用 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)

【问题讨论】:

    标签: r graphics lattice


    【解决方案1】:

    条形图需要单独的参数,而不是配对列表。我会做这样的事情:

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

    【讨论】:

    • 单个参数和pairlist有什么区别?
    • paired.f &lt;- function(list(x=1)) doSomeTasksinv.f &lt;- function(x=1) doSomeTasks
    • 所以... 是一个配对列表,而do.call(func, ...) 在列表中使用单独的参数?
    • 是的!这是正确的!请参阅 do.call(func,args,...) 其中 args 一个 list 函数调用的参数
    • 我想说原始代码不起作用的真正原因是 lattice:::barchart.formula 中的一个错误,它最终以某种方式调用了 lattice:::bwplot.formula - 该代码非常多毛。跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2016-12-22
    • 2013-10-31
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多