Skip to content

Latest commit

 

History

History
1443 lines (1375 loc) · 29.7 KB

rChartsLocalLib.md

File metadata and controls

1443 lines (1375 loc) · 29.7 KB

Markdown example with knitr and rCharts

=========================================== This is an example file showing how to use rCharts with knitr/markdown. rCharts an R package to create, customize and publish interactive javascript visualizations from R.

Let's first install it, it lives on github, not on CRAN yet.

require(devtools)
install_github("rCharts", "ramnathv", ref = "dev")

Scatter plot using Polycharts

require(rCharts)
## Loading required package: rCharts
names(iris) = gsub("\\.", "", names(iris))
add_lib_assets("Polycharts", cdn = TRUE)

[1] "<script type='text/javascript' src=https://rawgithub.com/Polychart/polychart2/develop/polychart2.standalone.js></script>"

r1 <- rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = "Species", 
    type = "point")
r1$print("polyScatter")
<script type='text/javascript'> var chartParams = { "dom": "polyScatter", "width": 800, "height": 400, "layers": [ { "x": "SepalWidth", "y": "SepalLength", "data": { "SepalLength": [ 5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5, 5, 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5, 5.5, 4.9, 4.4, 5.1, 5, 4.5, 4.4, 5, 5.1, 4.8, 5.1, 4.6, 5.3, 5, 7, 6.4, 6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5, 5.9, 6, 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6, 5.7, 5.5, 5.5, 5.8, 6, 5.4, 6, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8, 5, 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.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, 1.3, 1.3, 1.3, 1.2, 1.4, 1.2, 1, 1.3, 1.2, 1.3, 1.3, 1.1, 1.3, 2.5, 1.9, 2.1, 1.8, 2.2, 2.1, 1.7, 1.8, 1.8, 2.5, 2, 1.9, 2.1, 2, 2.4, 2.3, 1.8, 2.2, 2.3, 1.5, 2.3, 2, 2, 1.8, 2.1, 1.8, 1.8, 1.8, 2.1, 1.6, 1.9, 2, 2.2, 1.5, 1.4, 2.3, 2.4, 1.8, 1.8, 2.1, 2.4, 2.3, 1.9, 2.3, 2.5, 2.3, 1.9, 2, 2.3, 1.8 ], "Species": [ "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica" ] }, "facet": "Species", "color": "Species", "type": "point" } ], "facet": { "type": "wrap", "var": "Species" }, "guides": [], "coord": [], "id": "polyScatter" } _.each(chartParams.layers, function(el){ el.data = polyjs.data(el.data) }) var graph_polyScatter = polyjs.chart(chartParams); </script>

Multi barchart using NVD3

add_lib_assets("NVD3")  # get assets from online repositories 

[1] "\n\n<script type='text/javascript' src=/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/NVD3/js/jquery-1.8.2.min.js></script>\n<script type='text/javascript' src=/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/NVD3/js/d3.v3.min.js></script>\n<script type='text/javascript' src=/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/NVD3/js/nv.d3.min-new.js></script>\n<script type='text/javascript' src=/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/NVD3/js/fisheye.js></script>"

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
n1$chart(height = 200)  # change plot height Chrome/FF doesn't automatically get plot height but safari does
n1$print("nvd3mbar")
<script type='text/javascript'> $(document).ready(function(){ drawnvd3mbar() }); function drawnvd3mbar(){ var opts = { "dom": "nvd3mbar", "width": 800, "height": 400, "x": "Hair", "y": "Freq", "group": "Eye", "type": "multiBarChart", "id": "nvd3mbar" }, data = [ { "Hair": "Black", "Eye": "Brown", "Sex": "Male", "Freq": 32 }, { "Hair": "Brown", "Eye": "Brown", "Sex": "Male", "Freq": 53 }, { "Hair": "Red", "Eye": "Brown", "Sex": "Male", "Freq": 10 }, { "Hair": "Blond", "Eye": "Brown", "Sex": "Male", "Freq": 3 }, { "Hair": "Black", "Eye": "Blue", "Sex": "Male", "Freq": 11 }, { "Hair": "Brown", "Eye": "Blue", "Sex": "Male", "Freq": 50 }, { "Hair": "Red", "Eye": "Blue", "Sex": "Male", "Freq": 10 }, { "Hair": "Blond", "Eye": "Blue", "Sex": "Male", "Freq": 30 }, { "Hair": "Black", "Eye": "Hazel", "Sex": "Male", "Freq": 10 }, { "Hair": "Brown", "Eye": "Hazel", "Sex": "Male", "Freq": 25 }, { "Hair": "Red", "Eye": "Hazel", "Sex": "Male", "Freq": 7 }, { "Hair": "Blond", "Eye": "Hazel", "Sex": "Male", "Freq": 5 }, { "Hair": "Black", "Eye": "Green", "Sex": "Male", "Freq": 3 }, { "Hair": "Brown", "Eye": "Green", "Sex": "Male", "Freq": 15 }, { "Hair": "Red", "Eye": "Green", "Sex": "Male", "Freq": 7 }, { "Hair": "Blond", "Eye": "Green", "Sex": "Male", "Freq": 8 } ]
  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)
     
    chart

.height( 200)

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

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

Scatter Plot from NVD3

Scatter plot example with rCharts using NVD3 JS library

data(iris)
sepal <- iris[, c(1:2, 5)]

n2 <- nPlot(Sepal.Length ~ Sepal.Width, data = sepal, type = "scatterChart", 
    group = "Species")
n2$xAxis(axisLabel = "Sepal.Width")  # add x axis label
n2$yAxis(axisLabel = "Sepal.Length")
n2$chart(height = 400)  # change plot height Chrome/FF doesn't automatically get plot height but safari does
n2$print("nvd3Scatter")
<script type='text/javascript'> $(document).ready(function(){ drawnvd3Scatter() }); function drawnvd3Scatter(){ var opts = { "dom": "nvd3Scatter", "width": 800, "height": 400, "x": "Sepal.Width", "y": "Sepal.Length", "type": "scatterChart", "group": "Species", "id": "nvd3Scatter" }, data = [ { "Sepal.Length": 5.1, "Sepal.Width": 3.5, "Species": "setosa" }, { "Sepal.Length": 4.9, "Sepal.Width": 3, "Species": "setosa" }, { "Sepal.Length": 4.7, "Sepal.Width": 3.2, "Species": "setosa" }, { "Sepal.Length": 4.6, "Sepal.Width": 3.1, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3.6, "Species": "setosa" }, { "Sepal.Length": 5.4, "Sepal.Width": 3.9, "Species": "setosa" }, { "Sepal.Length": 4.6, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 4.4, "Sepal.Width": 2.9, "Species": "setosa" }, { "Sepal.Length": 4.9, "Sepal.Width": 3.1, "Species": "setosa" }, { "Sepal.Length": 5.4, "Sepal.Width": 3.7, "Species": "setosa" }, { "Sepal.Length": 4.8, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 4.8, "Sepal.Width": 3, "Species": "setosa" }, { "Sepal.Length": 4.3, "Sepal.Width": 3, "Species": "setosa" }, { "Sepal.Length": 5.8, "Sepal.Width": 4, "Species": "setosa" }, { "Sepal.Length": 5.7, "Sepal.Width": 4.4, "Species": "setosa" }, { "Sepal.Length": 5.4, "Sepal.Width": 3.9, "Species": "setosa" }, { "Sepal.Length": 5.1, "Sepal.Width": 3.5, "Species": "setosa" }, { "Sepal.Length": 5.7, "Sepal.Width": 3.8, "Species": "setosa" }, { "Sepal.Length": 5.1, "Sepal.Width": 3.8, "Species": "setosa" }, { "Sepal.Length": 5.4, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 5.1, "Sepal.Width": 3.7, "Species": "setosa" }, { "Sepal.Length": 4.6, "Sepal.Width": 3.6, "Species": "setosa" }, { "Sepal.Length": 5.1, "Sepal.Width": 3.3, "Species": "setosa" }, { "Sepal.Length": 4.8, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 5.2, "Sepal.Width": 3.5, "Species": "setosa" }, { "Sepal.Length": 5.2, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 4.7, "Sepal.Width": 3.2, "Species": "setosa" }, { "Sepal.Length": 4.8, "Sepal.Width": 3.1, "Species": "setosa" }, { "Sepal.Length": 5.4, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 5.2, "Sepal.Width": 4.1, "Species": "setosa" }, { "Sepal.Length": 5.5, "Sepal.Width": 4.2, "Species": "setosa" }, { "Sepal.Length": 4.9, "Sepal.Width": 3.1, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3.2, "Species": "setosa" }, { "Sepal.Length": 5.5, "Sepal.Width": 3.5, "Species": "setosa" }, { "Sepal.Length": 4.9, "Sepal.Width": 3.6, "Species": "setosa" }, { "Sepal.Length": 4.4, "Sepal.Width": 3, "Species": "setosa" }, { "Sepal.Length": 5.1, "Sepal.Width": 3.4, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3.5, "Species": "setosa" }, { "Sepal.Length": 4.5, "Sepal.Width": 2.3, "Species": "setosa" }, { "Sepal.Length": 4.4, "Sepal.Width": 3.2, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3.5, "Species": "setosa" }, { "Sepal.Length": 5.1, "Sepal.Width": 3.8, "Species": "setosa" }, { "Sepal.Length": 4.8, "Sepal.Width": 3, "Species": "setosa" }, { "Sepal.Length": 5.1, "Sepal.Width": 3.8, "Species": "setosa" }, { "Sepal.Length": 4.6, "Sepal.Width": 3.2, "Species": "setosa" }, { "Sepal.Length": 5.3, "Sepal.Width": 3.7, "Species": "setosa" }, { "Sepal.Length": 5, "Sepal.Width": 3.3, "Species": "setosa" }, { "Sepal.Length": 7, "Sepal.Width": 3.2, "Species": "versicolor" }, { "Sepal.Length": 6.4, "Sepal.Width": 3.2, "Species": "versicolor" }, { "Sepal.Length": 6.9, "Sepal.Width": 3.1, "Species": "versicolor" }, { "Sepal.Length": 5.5, "Sepal.Width": 2.3, "Species": "versicolor" }, { "Sepal.Length": 6.5, "Sepal.Width": 2.8, "Species": "versicolor" }, { "Sepal.Length": 5.7, "Sepal.Width": 2.8, "Species": "versicolor" }, { "Sepal.Length": 6.3, "Sepal.Width": 3.3, "Species": "versicolor" }, { "Sepal.Length": 4.9, "Sepal.Width": 2.4, "Species": "versicolor" }, { "Sepal.Length": 6.6, "Sepal.Width": 2.9, "Species": "versicolor" }, { "Sepal.Length": 5.2, "Sepal.Width": 2.7, "Species": "versicolor" }, { "Sepal.Length": 5, "Sepal.Width": 2, "Species": "versicolor" }, { "Sepal.Length": 5.9, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 6, "Sepal.Width": 2.2, "Species": "versicolor" }, { "Sepal.Length": 6.1, "Sepal.Width": 2.9, "Species": "versicolor" }, { "Sepal.Length": 5.6, "Sepal.Width": 2.9, "Species": "versicolor" }, { "Sepal.Length": 6.7, "Sepal.Width": 3.1, "Species": "versicolor" }, { "Sepal.Length": 5.6, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 5.8, "Sepal.Width": 2.7, "Species": "versicolor" }, { "Sepal.Length": 6.2, "Sepal.Width": 2.2, "Species": "versicolor" }, { "Sepal.Length": 5.6, "Sepal.Width": 2.5, "Species": "versicolor" }, { "Sepal.Length": 5.9, "Sepal.Width": 3.2, "Species": "versicolor" }, { "Sepal.Length": 6.1, "Sepal.Width": 2.8, "Species": "versicolor" }, { "Sepal.Length": 6.3, "Sepal.Width": 2.5, "Species": "versicolor" }, { "Sepal.Length": 6.1, "Sepal.Width": 2.8, "Species": "versicolor" }, { "Sepal.Length": 6.4, "Sepal.Width": 2.9, "Species": "versicolor" }, { "Sepal.Length": 6.6, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 6.8, "Sepal.Width": 2.8, "Species": "versicolor" }, { "Sepal.Length": 6.7, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 6, "Sepal.Width": 2.9, "Species": "versicolor" }, { "Sepal.Length": 5.7, "Sepal.Width": 2.6, "Species": "versicolor" }, { "Sepal.Length": 5.5, "Sepal.Width": 2.4, "Species": "versicolor" }, { "Sepal.Length": 5.5, "Sepal.Width": 2.4, "Species": "versicolor" }, { "Sepal.Length": 5.8, "Sepal.Width": 2.7, "Species": "versicolor" }, { "Sepal.Length": 6, "Sepal.Width": 2.7, "Species": "versicolor" }, { "Sepal.Length": 5.4, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 6, "Sepal.Width": 3.4, "Species": "versicolor" }, { "Sepal.Length": 6.7, "Sepal.Width": 3.1, "Species": "versicolor" }, { "Sepal.Length": 6.3, "Sepal.Width": 2.3, "Species": "versicolor" }, { "Sepal.Length": 5.6, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 5.5, "Sepal.Width": 2.5, "Species": "versicolor" }, { "Sepal.Length": 5.5, "Sepal.Width": 2.6, "Species": "versicolor" }, { "Sepal.Length": 6.1, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 5.8, "Sepal.Width": 2.6, "Species": "versicolor" }, { "Sepal.Length": 5, "Sepal.Width": 2.3, "Species": "versicolor" }, { "Sepal.Length": 5.6, "Sepal.Width": 2.7, "Species": "versicolor" }, { "Sepal.Length": 5.7, "Sepal.Width": 3, "Species": "versicolor" }, { "Sepal.Length": 5.7, "Sepal.Width": 2.9, "Species": "versicolor" }, { "Sepal.Length": 6.2, "Sepal.Width": 2.9, "Species": "versicolor" }, { "Sepal.Length": 5.1, "Sepal.Width": 2.5, "Species": "versicolor" }, { "Sepal.Length": 5.7, "Sepal.Width": 2.8, "Species": "versicolor" }, { "Sepal.Length": 6.3, "Sepal.Width": 3.3, "Species": "virginica" }, { "Sepal.Length": 5.8, "Sepal.Width": 2.7, "Species": "virginica" }, { "Sepal.Length": 7.1, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 6.3, "Sepal.Width": 2.9, "Species": "virginica" }, { "Sepal.Length": 6.5, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 7.6, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 4.9, "Sepal.Width": 2.5, "Species": "virginica" }, { "Sepal.Length": 7.3, "Sepal.Width": 2.9, "Species": "virginica" }, { "Sepal.Length": 6.7, "Sepal.Width": 2.5, "Species": "virginica" }, { "Sepal.Length": 7.2, "Sepal.Width": 3.6, "Species": "virginica" }, { "Sepal.Length": 6.5, "Sepal.Width": 3.2, "Species": "virginica" }, { "Sepal.Length": 6.4, "Sepal.Width": 2.7, "Species": "virginica" }, { "Sepal.Length": 6.8, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 5.7, "Sepal.Width": 2.5, "Species": "virginica" }, { "Sepal.Length": 5.8, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 6.4, "Sepal.Width": 3.2, "Species": "virginica" }, { "Sepal.Length": 6.5, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 7.7, "Sepal.Width": 3.8, "Species": "virginica" }, { "Sepal.Length": 7.7, "Sepal.Width": 2.6, "Species": "virginica" }, { "Sepal.Length": 6, "Sepal.Width": 2.2, "Species": "virginica" }, { "Sepal.Length": 6.9, "Sepal.Width": 3.2, "Species": "virginica" }, { "Sepal.Length": 5.6, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 7.7, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 6.3, "Sepal.Width": 2.7, "Species": "virginica" }, { "Sepal.Length": 6.7, "Sepal.Width": 3.3, "Species": "virginica" }, { "Sepal.Length": 7.2, "Sepal.Width": 3.2, "Species": "virginica" }, { "Sepal.Length": 6.2, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 6.1, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 6.4, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 7.2, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 7.4, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 7.9, "Sepal.Width": 3.8, "Species": "virginica" }, { "Sepal.Length": 6.4, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 6.3, "Sepal.Width": 2.8, "Species": "virginica" }, { "Sepal.Length": 6.1, "Sepal.Width": 2.6, "Species": "virginica" }, { "Sepal.Length": 7.7, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 6.3, "Sepal.Width": 3.4, "Species": "virginica" }, { "Sepal.Length": 6.4, "Sepal.Width": 3.1, "Species": "virginica" }, { "Sepal.Length": 6, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 6.9, "Sepal.Width": 3.1, "Species": "virginica" }, { "Sepal.Length": 6.7, "Sepal.Width": 3.1, "Species": "virginica" }, { "Sepal.Length": 6.9, "Sepal.Width": 3.1, "Species": "virginica" }, { "Sepal.Length": 5.8, "Sepal.Width": 2.7, "Species": "virginica" }, { "Sepal.Length": 6.8, "Sepal.Width": 3.2, "Species": "virginica" }, { "Sepal.Length": 6.7, "Sepal.Width": 3.3, "Species": "virginica" }, { "Sepal.Length": 6.7, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 6.3, "Sepal.Width": 2.5, "Species": "virginica" }, { "Sepal.Length": 6.5, "Sepal.Width": 3, "Species": "virginica" }, { "Sepal.Length": 6.2, "Sepal.Width": 3.4, "Species": "virginica" }, { "Sepal.Length": 5.9, "Sepal.Width": 3, "Species": "virginica" } ]
  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)
     
    chart

.height( 400)

    chart.xAxis

.axisLabel("Sepal.Width")

    chart.yAxis

.axisLabel("Sepal.Length")

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

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

Histogram Plot from NVD3

Let's try to plot a multihistogram with rCharts using NVD3 library. We need to first calculate break points and mid points for the histogram bars and produce a single data frame that has the counts, mid-points for bars and group information.

data(iris)
sepalw <- iris[, c(1, 5)]
hst = hist(sepalw[, 1], plot = FALSE, breaks = 20)

data = by(sepalw, sepalw$Species, function(x) data.frame(mid = hst$mids, counts = hist(x[, 
    1], breaks = hst$breaks, plot = FALSE)$counts, Species = rep(x[1, 2], length(hst$breaks) - 
    1)))
data = do.call("rbind", data)
head(data)
##          mid counts Species
## setosa.1 4.3      4  setosa
## setosa.2 4.5      5  setosa
## setosa.3 4.7      7  setosa
## setosa.4 4.9     12  setosa
## setosa.5 5.1     11  setosa
## setosa.6 5.3      6  setosa

We got the data in the right format, now let's plot the histogram with multiBarChart

n3 <- nPlot(counts ~ mid, data = data, type = "multiBarChart", group = "Species")
n3$xAxis(axisLabel = "Sepal.Width")
n3$yAxis(axisLabel = "counts")
n3$chart(color = c("red", "blue", "green"))
n2$chart(height = 400)  # change plot height Chrome/FF doesn't automatically get plot height but safari does
n3$print("nvd3Hist")
<script type='text/javascript'> $(document).ready(function(){ drawnvd3Hist() }); function drawnvd3Hist(){ var opts = { "dom": "nvd3Hist", "width": 800, "height": 400, "x": "mid", "y": "counts", "type": "multiBarChart", "group": "Species", "id": "nvd3Hist" }, data = [ { "mid": 4.3, "counts": 4, "Species": "setosa" }, { "mid": 4.5, "counts": 5, "Species": "setosa" }, { "mid": 4.7, "counts": 7, "Species": "setosa" }, { "mid": 4.9, "counts": 12, "Species": "setosa" }, { "mid": 5.1, "counts": 11, "Species": "setosa" }, { "mid": 5.3, "counts": 6, "Species": "setosa" }, { "mid": 5.5, "counts": 2, "Species": "setosa" }, { "mid": 5.7, "counts": 3, "Species": "setosa" }, { "mid": 5.9, "counts": 0, "Species": "setosa" }, { "mid": 6.1, "counts": 0, "Species": "setosa" }, { "mid": 6.3, "counts": 0, "Species": "setosa" }, { "mid": 6.5, "counts": 0, "Species": "setosa" }, { "mid": 6.7, "counts": 0, "Species": "setosa" }, { "mid": 6.9, "counts": 0, "Species": "setosa" }, { "mid": 7.1, "counts": 0, "Species": "setosa" }, { "mid": 7.3, "counts": 0, "Species": "setosa" }, { "mid": 7.5, "counts": 0, "Species": "setosa" }, { "mid": 7.7, "counts": 0, "Species": "setosa" }, { "mid": 7.9, "counts": 0, "Species": "setosa" }, { "mid": 4.3, "counts": 0, "Species": "versicolor" }, { "mid": 4.5, "counts": 0, "Species": "versicolor" }, { "mid": 4.7, "counts": 0, "Species": "versicolor" }, { "mid": 4.9, "counts": 3, "Species": "versicolor" }, { "mid": 5.1, "counts": 2, "Species": "versicolor" }, { "mid": 5.3, "counts": 1, "Species": "versicolor" }, { "mid": 5.5, "counts": 10, "Species": "versicolor" }, { "mid": 5.7, "counts": 8, "Species": "versicolor" }, { "mid": 5.9, "counts": 6, "Species": "versicolor" }, { "mid": 6.1, "counts": 6, "Species": "versicolor" }, { "mid": 6.3, "counts": 5, "Species": "versicolor" }, { "mid": 6.5, "counts": 3, "Species": "versicolor" }, { "mid": 6.7, "counts": 4, "Species": "versicolor" }, { "mid": 6.9, "counts": 2, "Species": "versicolor" }, { "mid": 7.1, "counts": 0, "Species": "versicolor" }, { "mid": 7.3, "counts": 0, "Species": "versicolor" }, { "mid": 7.5, "counts": 0, "Species": "versicolor" }, { "mid": 7.7, "counts": 0, "Species": "versicolor" }, { "mid": 7.9, "counts": 0, "Species": "versicolor" }, { "mid": 4.3, "counts": 0, "Species": "virginica" }, { "mid": 4.5, "counts": 0, "Species": "virginica" }, { "mid": 4.7, "counts": 0, "Species": "virginica" }, { "mid": 4.9, "counts": 1, "Species": "virginica" }, { "mid": 5.1, "counts": 0, "Species": "virginica" }, { "mid": 5.3, "counts": 0, "Species": "virginica" }, { "mid": 5.5, "counts": 1, "Species": "virginica" }, { "mid": 5.7, "counts": 4, "Species": "virginica" }, { "mid": 5.9, "counts": 3, "Species": "virginica" }, { "mid": 6.1, "counts": 4, "Species": "virginica" }, { "mid": 6.3, "counts": 11, "Species": "virginica" }, { "mid": 6.5, "counts": 4, "Species": "virginica" }, { "mid": 6.7, "counts": 7, "Species": "virginica" }, { "mid": 6.9, "counts": 3, "Species": "virginica" }, { "mid": 7.1, "counts": 4, "Species": "virginica" }, { "mid": 7.3, "counts": 2, "Species": "virginica" }, { "mid": 7.5, "counts": 1, "Species": "virginica" }, { "mid": 7.7, "counts": 4, "Species": "virginica" }, { "mid": 7.9, "counts": 1, "Species": "virginica" } ]
  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)
     
    chart

.color([ "red", "blue", "green" ])

    chart.xAxis

.axisLabel("Sepal.Width")

    chart.yAxis

.axisLabel("counts")

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

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