{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions.\n", "\n", "Install the mechanize gem version ~>2.7.5 for using mechanize functions.\n" ] }, { "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\":\"http://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\", \"http://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\\\":\\\"http://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\\\", \\\"http://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": [ "true" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'daru/view'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Categorical Vector Visualization" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\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", "
Daru::Vector(6):category
0a
1a
2a
3b
4b
5c
" ], "text/plain": [ "#\n", " 0 a\n", " 1 a\n", " 2 a\n", " 3 b\n", " 4 b\n", " 5 c" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new [:a, :a, :a, :b, :b, :c], type: :category" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bar graph\n", "\n", "##### 1. Frequency (count)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"f71ed3e1-f71c-4d31-8b9c-87afa980bd26\"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{}}>>" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph1 = Daru::View::Plot.new(dv, type: :bar)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Nyaplot::Plot" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph1.chart.class" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"f71ed3e1-f71c-4d31-8b9c-87afa980bd26\"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{:width=>700, :xrange=>[:a, :b, :c], :yrange=>[0, 3]}}>], :data=>{\"f71ed3e1-f71c-4d31-8b9c-87afa980bd26\"=>#:a, :data1=>3}, {:data0=>:b, :data1=>2}, {:data0=>:c, :data1=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bar_graph1.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new ['III']*10 + ['II']*5 + ['I']*5, type: :category, categories: ['I', 'II', 'III']\n", "dv.type" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"0b4f6d25-5c36-4692-be4b-9e990e52524c\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 10]>], :options=>{}}>>" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2 = Daru::View::Plot.new(dv, type: :bar)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Daru::View::Plot" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2.class" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Nyaplot::Plot" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2.chart.class" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"0b4f6d25-5c36-4692-be4b-9e990e52524c\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 10]>], :options=>{:x_label=>\"Cat\", :width=>700, :xrange=>[\"I\", \"II\", \"III\"], :yrange=>[0, 10], :y_label=>\"Frequency\"}}>" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Nyaplot methods will work.Since #chart is Nyaplot::Plot class object\n", "bar_graph2.chart.x_label 'Cat'\n", "bar_graph2.chart.y_label 'Frequency'" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"0b4f6d25-5c36-4692-be4b-9e990e52524c\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 10]>], :options=>{:x_label=>\"Cat\", :width=>700, :xrange=>[\"I\", \"II\", \"III\"], :yrange=>[0, 10], :y_label=>\"Frequency\"}}>" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2.chart # or bar_graph2.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Percentage" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"2d91e56c-fdbc-4d3c-acc1-25d9ac498ff1\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 50.0]>], :options=>{:yrange=>[0, 100]}}>>" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2_per = Daru::View::Plot.new(dv, type: :bar, method: :percentage)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"2d91e56c-fdbc-4d3c-acc1-25d9ac498ff1\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 50.0]>], :options=>{:yrange=>[0, 100], :width=>700, :xrange=>[\"I\", \"II\", \"III\"], :x_label=>\"Categories\", :y_label=>\"Percentage (%)\"}}>], :data=>{\"2d91e56c-fdbc-4d3c-acc1-25d9ac498ff1\"=>#\"I\", :data1=>25.0}, {:data0=>\"II\", :data1=>25.0}, {:data0=>\"III\", :data1=>50.0}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bar_graph2_per.chart.x_label 'Categories'\n", "bar_graph2_per.chart.y_label 'Percentage (%)'\n", "bar_graph2_per.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Fraction" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"de67e9c8-89a1-4ebc-9168-78c1a254f5d3\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 0.5]>], :options=>{:yrange=>[0, 1]}}>>" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2_frac = Daru::View::Plot.new(dv, type: :bar, method: :fraction)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"de67e9c8-89a1-4ebc-9168-78c1a254f5d3\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 0.5]>], :options=>{:yrange=>[0, 1], :x_label=>\"Categories\", :y_label=>\"Fraction\", :width=>700, :xrange=>[\"I\", \"II\", \"III\"]}}>], :data=>{\"de67e9c8-89a1-4ebc-9168-78c1a254f5d3\"=>#\"I\", :data1=>0.25}, {:data0=>\"II\", :data1=>0.25}, {:data0=>\"III\", :data1=>0.5}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# bar_graph2_frac.chart.x_label 'Categories'\n", "bar_graph2_frac.chart.y_label 'Fraction'\n", "bar_graph2_frac.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Categorical data visualization in Dataframe\n", "### Bar Graph" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = Daru::DataFrame.new({\n", " a: [1, 2, 4, -2, 5, 23, 0],\n", " b: [3, 1, 3, -6, 2, 1, 0],\n", " c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']\n", " })\n", "df.to_category :c\n", "df[:c].type" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#[#:bar, :options=>{:value=>:c}, :data=>\"c1c752e6-e52f-4acf-b295-752e8427507d\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 7]>], :options=>{}}>>" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph3 = Daru::View::Plot.new(df, type: :bar, x: :c)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:value=>:c}, :data=>\"c1c752e6-e52f-4acf-b295-752e8427507d\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 7]>], :options=>{:width=>700, :xrange=>[\"I\", \"II\", \"III\"], :yrange=>[0, 7]}}>], :data=>{\"c1c752e6-e52f-4acf-b295-752e8427507d\"=>#1, :b=>3, :c=>\"I\"}, {:a=>2, :b=>1, :c=>\"II\"}, {:a=>4, :b=>3, :c=>\"I\"}, {:a=>-2, :b=>-6, :c=>\"III\"}, {:a=>5, :b=>2, :c=>\"I\"}, {:a=>23, :b=>1, :c=>\"III\"}, {:a=>0, :b=>0, :c=>\"II\"}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bar_graph3.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scatter plot categorized by categorical variable¶\n", "#### Plots can be categorized by\n", "\n", "- Color\n", "- Size\n", "- Shape" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = Daru::DataFrame.new({\n", " a: [1, 2, 4, -2, 5, 23, 0],\n", " b: [3, 1, 3, -6, 2, 1, 0],\n", " c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']\n", " })\n", "df.to_category :c\n", "df[:c].type" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(179,226,205)\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"98c86027-999b-4c4c-bbfc-c618a33552ce\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(253,205,172)\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"fb73976e-b3ed-4c03-a61d-83b77bcab567\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(203,213,232)\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"c1bf3eb7-5da0-484b-8fdc-2630ae05d027\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true}}>>" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_1 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color})" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(179,226,205)\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"98c86027-999b-4c4c-bbfc-c618a33552ce\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(253,205,172)\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"fb73976e-b3ed-4c03-a61d-83b77bcab567\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(203,213,232)\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"c1bf3eb7-5da0-484b-8fdc-2630ae05d027\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"98c86027-999b-4c4c-bbfc-c618a33552ce\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"fb73976e-b3ed-4c03-a61d-83b77bcab567\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"c1bf3eb7-5da0-484b-8fdc-2630ae05d027\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "scatter_1.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(179,226,205)\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"98c86027-999b-4c4c-bbfc-c618a33552ce\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(253,205,172)\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"fb73976e-b3ed-4c03-a61d-83b77bcab567\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(203,213,232)\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"c1bf3eb7-5da0-484b-8fdc-2630ae05d027\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_1.chart.xrange [-10, 10]\n", "scatter_1.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :shape=>\"circle\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"36aa48a1-b3ab-4eb1-b618-66c4fd718126\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :shape=>\"triangle-up\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"38531a02-1349-472f-ae14-9cf631544111\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :shape=>\"diamond\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"5b52cce3-1ed8-496c-bd5a-3ea0a0dc7c34\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"36aa48a1-b3ab-4eb1-b618-66c4fd718126\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"38531a02-1349-472f-ae14-9cf631544111\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"5b52cce3-1ed8-496c-bd5a-3ea0a0dc7c34\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "scatter_2 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :shape})\n", "scatter_2.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :shape=>\"circle\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"36aa48a1-b3ab-4eb1-b618-66c4fd718126\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :shape=>\"triangle-up\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"38531a02-1349-472f-ae14-9cf631544111\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :shape=>\"diamond\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"5b52cce3-1ed8-496c-bd5a-3ea0a0dc7c34\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_2.chart.xrange [-10, 10]\n", "scatter_2.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>:red, :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"fd875fc6-c338-4830-bf1c-64bb4933fbb4\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>:blue, :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"90a5de38-5153-4e70-8847-e77f51492989\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>:green, :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"71c914ea-3511-4cf0-acf6-16e488a93f83\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"fd875fc6-c338-4830-bf1c-64bb4933fbb4\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"90a5de38-5153-4e70-8847-e77f51492989\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"71c914ea-3511-4cf0-acf6-16e488a93f83\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :shape=>\"circle\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"36aa48a1-b3ab-4eb1-b618-66c4fd718126\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :shape=>\"triangle-up\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"38531a02-1349-472f-ae14-9cf631544111\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :shape=>\"diamond\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"5b52cce3-1ed8-496c-bd5a-3ea0a0dc7c34\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_3 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color, color: [:red, :blue, :green]})\n", "scatter_3.show_in_iruby\n", "scatter_2.chart.xrange [-10, 10]\n", "scatter_2.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :size=>300, :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"3d1a62a0-79bc-4fe0-8bfd-62833dfbca5c\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :size=>600, :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"96c2db80-6619-4529-951a-4e2790942d05\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :size=>900, :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"16555dec-9187-4c56-9fc2-8365b8eb547f\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"3d1a62a0-79bc-4fe0-8bfd-62833dfbca5c\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"96c2db80-6619-4529-951a-4e2790942d05\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"16555dec-9187-4c56-9fc2-8365b8eb547f\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :size=>300, :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"3d1a62a0-79bc-4fe0-8bfd-62833dfbca5c\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :size=>600, :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"96c2db80-6619-4529-951a-4e2790942d05\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :size=>900, :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"16555dec-9187-4c56-9fc2-8365b8eb547f\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_4 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :size, size: [300, 600, 900]})\n", "scatter_4.show_in_iruby\n", "scatter_4.chart.xrange [-10, 10]\n", "scatter_4.chart.yrange [-10, 10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Line plot categorized by categorical variable\n", "It works similar to Scatter plot above and all options are same except that there's no categorization by size but instead there is categorization by stroke_width in line plots." ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = Daru::DataFrame.new({\n", " a: [1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " b: [2, 4, 6, 1, 3, 5, 6, 4, 3],\n", " c: ['I']*3 + ['II']*3 + ['III']*3\n", " })\n", "df.to_category :c\n", "df[:c].type" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:line, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(127,201,127)\"}, :data=>\"df22e4b3-6afb-4438-86f3-bb346e1448a5\"}, @xrange=[1, 3], @yrange=[2, 6]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(190,174,212)\"}, :data=>\"44813aed-1aff-4294-84a9-0e3aeb2de58e\"}, @xrange=[4, 6], @yrange=[1, 5]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(253,192,134)\"}, :data=>\"b8f79462-93ff-45b3-931a-9c89d06ed1b7\"}, @xrange=[7, 9], @yrange=[3, 6]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[1, 9], :yrange=>[1, 6]}}>], :data=>{\"df22e4b3-6afb-4438-86f3-bb346e1448a5\"=>#1, :b=>2}, {:a=>2, :b=>4}, {:a=>3, :b=>6}]>, \"44813aed-1aff-4294-84a9-0e3aeb2de58e\"=>#4, :b=>1}, {:a=>5, :b=>3}, {:a=>6, :b=>5}]>, \"b8f79462-93ff-45b3-931a-9c89d06ed1b7\"=>#7, :b=>6}, {:a=>8, :b=>4}, {:a=>9, :b=>3}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "line_1 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :color})\n", "line_1.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:line, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :stroke_width=>2}, :data=>\"66fb44aa-c3e8-4de7-806a-2e36475d8c17\"}, @xrange=[1, 3], @yrange=[2, 6]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :stroke_width=>4}, :data=>\"517311c1-b083-4141-8548-9a5da9f99bec\"}, @xrange=[4, 6], @yrange=[1, 5]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :stroke_width=>6}, :data=>\"1e279133-872f-4e6e-8b8d-8be26ce143b8\"}, @xrange=[7, 9], @yrange=[3, 6]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[1, 9], :yrange=>[1, 6]}}>], :data=>{\"66fb44aa-c3e8-4de7-806a-2e36475d8c17\"=>#1, :b=>2}, {:a=>2, :b=>4}, {:a=>3, :b=>6}]>, \"517311c1-b083-4141-8548-9a5da9f99bec\"=>#4, :b=>1}, {:a=>5, :b=>3}, {:a=>6, :b=>5}]>, \"1e279133-872f-4e6e-8b8d-8be26ce143b8\"=>#7, :b=>6}, {:a=>8, :b=>4}, {:a=>9, :b=>3}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:line, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :stroke_width=>2}, :data=>\"66fb44aa-c3e8-4de7-806a-2e36475d8c17\"}, @xrange=[1, 3], @yrange=[2, 6]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :stroke_width=>4}, :data=>\"517311c1-b083-4141-8548-9a5da9f99bec\"}, @xrange=[4, 6], @yrange=[1, 5]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :stroke_width=>6}, :data=>\"1e279133-872f-4e6e-8b8d-8be26ce143b8\"}, @xrange=[7, 9], @yrange=[3, 6]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line_2 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :stroke_width})\n", "line_2.show_in_iruby\n", "line_2.chart.xrange [-10, 10]\n", "line_2.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.3.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.3.1" } }, "nbformat": 4, "nbformat_minor": 2 }