2

I tried to use rChart's rNVD3 package's discrete bar plot instead of the same old ggplot2. But it's requiring some formula argument as a first argument. I've not used lattice package and I don't know how to create one.

Here's my data frame:

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

Code I used to render the plot:

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

This is what the error is:

Error in lattice::latticeParseFormula(x, data = data) : model must be a formula object

When I tried to rectify the error:

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

It just showed the .js code but not the barchart.

<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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Solomon AathiRaj
  • 119
  • 1
  • 10
  • Try `nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600)`. The error message is quite clear. –  Feb 25 '16 at 21:05
  • I'm not getting even if I run your LOC. Output is not getting rendered. It shows no error too. Instead the above mentioned js code appears. @Pascal – Solomon AathiRaj Feb 26 '16 at 04:17
  • It is not what I said. If I **really** need to write everything: `bar1 <- nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600); bar1$printChart("chart1")` –  Feb 26 '16 at 04:57
  • That's what I tried for @Pascal It didn't work. – Solomon AathiRaj Feb 26 '16 at 06:47
  • According to your edit, it is not. I give up. I said `bar1 <- nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600); bar1$printChart("chart1")`, not `nvd3Plot(Scores ~ Pupil, data = df, type = "discreteBarChart", width = 600); bar1$printChart("chart1")` –  Feb 26 '16 at 09:48
  • My extreme apologies @Pascal I didn't include variable only when showing here. But I ran the code with the variable only. No change. Anyways, thanks for your great help. Will handle this issue somehow. – Solomon AathiRaj Feb 26 '16 at 10:59

1 Answers1

1

rCharts can be a little confusing sometimes due to its use of reference classes. You are very close. First install rCharts. Then, instead of nvd3Plot, use nPlot as shown below. Also, you might be interested in 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)
timelyportfolio
  • 6,479
  • 30
  • 33