【问题标题】:Error in lattice::latticeParseFormula(x, data = data) : model must be a formula objectlattice::latticeParseFormula(x, data = data) 中的错误:模型必须是公式对象
【发布时间】:2016-02-25 20:30:31
【问题描述】:

我尝试使用 rChart 的 rNVD3 包的离散条形图而不是相同的旧 ggplot2。但它需要一些公式参数作为第一个参数。我没用过 lattice 包,也不知道怎么创建。

这是我的数据框:

   df <- data.frame(
      Pupil = factor(c("Richy","Shyam","Nithin"), levels=c("Richy","Shyam","Nithin")), 
      Scores = c(75,93,62)
      )

我用来渲染情节的代码:

  require(rNVD3)
    bar1 <- nvd3Plot(x = "Pupil", y = "Scores", data = df, type = "discreteBarChart", width = 600)
    bar1$printChart("chart1")

这就是错误:

lattice::latticeParseFormula(x, data = data) 中的错误: 模型必须是公式对象

当我试图纠正错误时:

 bar1<-nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600)
    bar1$printChart("chart1")

它只显示 .js 代码而不是条形图。

<div id='chart1' class='nvd3Plot'></div>
    <script type='text/javascript'>
        drawchart1()
        function drawchart1(){  
          var opts = {"id":"chart1","yAxis":[],"x":"Pupil","y":"Scores","type":"discreteBarChart","width":600,"height":400},
            data = [{"Pupil":"Richy","Scores":75},{"Pupil":"Shyam","Scores":93},{"Pupil":"Nithin","Scores":62}]

          var data = d3.nest()
            .key(function(d){
              return opts.group === undefined ? 'main' : d[opts.group]
            })
            .entries(data)

          nv.addGraph(function() {
            var chart = nv.models[opts.type]()
              .x(function(d) { return d[opts.x] })
              .y(function(d) { return d[opts.y] })
              .width(opts.width)
              .height(opts.height)

           d3.select("#" + opts.id)
            .append('svg')
            .datum(data)
            .transition().duration(500)
            .call(chart);

           nv.utils.windowResize(chart.update);
           return chart;
          });
    };
</script>

【问题讨论】:

  • 试试nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600)。错误信息很清楚。
  • 即使我运行你的 LOC,我也没有得到。输出未呈现。它也没有显示错误。而是出现了上面提到的js代码。 @帕斯卡
  • 不是我说的。如果我真的需要写所有东西:bar1 &lt;- nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600); bar1$printChart("chart1")
  • 这就是我为@Pascal 尝试过的,但没有用。
  • 根据您的编辑,它不是。我放弃。我说的是bar1 &lt;- nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600); bar1$printChart("chart1"),不是nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600); bar1$printChart("chart1")

标签: r rcharts


【解决方案1】:

rCharts 有时会因为使用reference classes 而有点令人困惑。你很亲密。首先安装rCharts. 然后,而不是nvd3Plot,使用nPlot,如下所示。另外,您可能对htmlwidgets 感兴趣。

library(rCharts)

df <- data.frame(
  Pupil = factor(c("Richy","Shyam","Nithin"), levels=c("Richy","Shyam","Nithin")), 
  Scores = c(75,93,62)
)

# without formula interface
nPlot(
  x = "Pupil", y = "Scores", data = df,
  type = "discreteBarChart", width = 600
)

# with formula interface
nPlot(Scores~Pupil, data = df, type = "discreteBarChart", width = 600)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2018-02-23
    • 2018-12-26
    • 1970-01-01
    • 2020-08-18
    相关资源
    最近更新 更多