{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Boxplots with daru and statsample\n", "\n", "### Description\n", "\n", "This example illustrates how daru, combined with Statsample::Graph::Boxplot\n", "can be used for generating box plots of a normally distributed set of data.\n", "\n", "The 'rnorm' function, defined in statsample/shorthands generates a Daru::Vector\n", "object which contains the specified number of random variables in a normal distribution.\n", "It uses the 'distribution' gem for this purpose." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "application/javascript": [ "if(window['d3'] === undefined ||\n", " window['Nyaplot'] === undefined){\n", " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", "\n", "\n", "\n", " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", "\n", " require.config({paths: path, shim:shim});\n", "\n", "\n", "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", "\n", "\tvar script = d3.select(\"head\")\n", "\t .append(\"script\")\n", "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", "\t .attr(\"async\", true);\n", "\n", "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", "\n", "\n", "\t var event = document.createEvent(\"HTMLEvents\");\n", "\t event.initEvent(\"load_nyaplot\",false,false);\n", "\t window.dispatchEvent(event);\n", "\t console.log('Finished loading Nyaplotjs');\n", "\n", "\t};\n", "\n", "\n", "});});\n", "}\n" ], "text/plain": [ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Daru::Vector" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'daru'\n", "require 'statsample'\n", "include Statsample::Shorthand\n", "\n", "n = 30\n", "a = rnorm(n-1,50,10)\n", "b = rnorm(n, 30,5)\n", "c = rnorm(n,5,1)\n", "\n", "# Append 2 to vector 'a' at the end\n", "a.push(2, a.size)\n", "\n", "a.class" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"f8db82aa-14f7-496a-ad30-6e77a9024573\"}, @xrange=[0, 29], @yrange=[2, 68.08468144277012]>], :options=>{:zoom=>true, :width=>700, :xrange=>[0, 29], :yrange=>[2, 68.08468144277012]}}>], :data=>{\"f8db82aa-14f7-496a-ad30-6e77a9024573\"=>#0, :data1=>37.59292749591977}, {:data0=>1, :data1=>58.63959969073256}, {:data0=>2, :data1=>49.72096172683134}, {:data0=>3, :data1=>68.08468144277012}, {:data0=>4, :data1=>41.18498609152795}, {:data0=>5, :data1=>53.76175930320878}, {:data0=>6, :data1=>50.58405555791411}, {:data0=>7, :data1=>54.43404614419645}, {:data0=>8, :data1=>63.31006277887309}, {:data0=>9, :data1=>53.17315998661855}, {:data0=>10, :data1=>54.77448250840547}, {:data0=>11, :data1=>41.022842686930844}, {:data0=>12, :data1=>56.47532980545696}, {:data0=>13, :data1=>39.942082771431274}, {:data0=>14, :data1=>60.265077311589636}, {:data0=>15, :data1=>40.07698735253937}, {:data0=>16, :data1=>37.53949543863687}, {:data0=>17, :data1=>51.45038593391677}, {:data0=>18, :data1=>48.13146513902144}, {:data0=>19, :data1=>50.60141771849789}, {:data0=>20, :data1=>53.54622017454528}, {:data0=>21, :data1=>52.87655448401752}, {:data0=>22, :data1=>35.74695727012832}, {:data0=>23, :data1=>39.85848942106581}, {:data0=>24, :data1=>61.52656071654289}, {:data0=>25, :data1=>33.216665144563876}, {:data0=>26, :data1=>48.39622785608005}, {:data0=>27, :data1=>28.577848346219746}, {:data0=>28, :data1=>47.26737348315691}, {:data0=>29, :data1=>2}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "a.plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create a boxplot of the data by specifying the vectors a, b and c and providing necessary options to Statsample::Graph::Boxplot. \n", "\n", "#### The `#to_svg` function returns the boxplot image in a raw SVG format. To display this properly in iruby we need to pass that output to the `IRuby.svg` method." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " 0\n", " 10\n", " 20\n", " 30\n", " 40\n", " 50\n", " 60\n", " 70\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ], "text/plain": [ "\"\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n 0\\n 10\\n 20\\n 30\\n 40\\n 50\\n 60\\n 70\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n\"" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IRuby.svg boxplot(:vectors=>[a,b,c],:width=>300, :height=>300, :groups=>%w{first first second}, :minimum=>0).to_svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IRuby.svg boxplot(:vectors=>[a,b,c],:width=>300, :height=>300, :groups=>%w{first first second}, :minimum=>0).to_svg" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.2.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.2.1" } }, "nbformat": 4, "nbformat_minor": 0 }