In [1]:
require 'daru/view'
Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions.

Install the mechanize gem version ~>2.7.5 for using mechanize functions.
Out[1]:
true
In [6]:
Daru::View.plotting_library = :nyaplot
Out[6]:
:nyaplot

Categorical Vector Visualization

In [7]:
dv = Daru::Vector.new [:a, :a, :a, :b, :b, :c], type: :category
Out[7]:
Daru::Vector(6):category
0 a
1 a
2 a
3 b
4 b
5 c

Bar graph

1. Frequency (count)
In [8]:
bar_graph1 = Daru::View::Plot.new(dv, type: :bar)
Out[8]:
#<Daru::View::Plot:0x007fd05c935a10 @data=#<Daru::Vector(6):cataegory>
   0   a
   1   a
   2   a
   3   b
   4   b
   5   c, @options={:type=>:bar, :method=>:count}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fd05c935830 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fd05c92cc80 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"da25c1e7-7aad-4496-954d-2b4ebd49b531"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{}}>>
In [9]:
bar_graph1.chart.class
Out[9]:
Nyaplot::Plot
In [10]:
bar_graph1.show_in_iruby
In [11]:
dv = Daru::Vector.new ['III']*10 + ['II']*5 + ['I']*5, type: :category, categories: ['I', 'II', 'III']
dv.type
Out[11]:
:category
In [12]:
bar_graph2 = Daru::View::Plot.new(dv, type: :bar)
Out[12]:
#<Daru::View::Plot:0x007fd05b2faf20 @data=#<Daru::Vector(20):cataegory>
   0 III
   1 III
   2 III
   3 III
   4 III
   5 III
   6 III
   7 III
   8 III
   9 III
  10  II
  11  II
  12  II
  13  II
  14  II
 ... ..., @options={:type=>:bar, :method=>:count}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fd05b2fae30 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fd05b2f90d0 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"9e71388d-9a02-42cd-adcc-fd532045edf3"}, @xrange=["I", "II", "III"], @yrange=[0, 10]>], :options=>{}}>>
In [13]:
bar_graph2.class
Out[13]:
Daru::View::Plot
In [14]:
bar_graph2.chart.class
Out[14]:
Nyaplot::Plot
In [15]:
# Nyaplot methods will work.Since #chart is Nyaplot::Plot class object
bar_graph2.chart.x_label 'Cat'
bar_graph2.chart.y_label 'Frequency'
Out[15]:
In [16]:
bar_graph2.chart # or bar_graph2.show_in_iruby
Out[16]:

2. Percentage

In [17]:
bar_graph2_per = Daru::View::Plot.new(dv, type: :bar, method: :percentage)
Out[17]:
#<Daru::View::Plot:0x007fd05c34a678 @data=#<Daru::Vector(20):cataegory>
   0 III
   1 III
   2 III
   3 III
   4 III
   5 III
   6 III
   7 III
   8 III
   9 III
  10  II
  11  II
  12  II
  13  II
  14  II
 ... ..., @options={:type=>:bar, :method=>:percentage}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fd05c34be10 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fd05b29a030 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"decb6897-b99b-420e-861d-bbe7a8375714"}, @xrange=["I", "II", "III"], @yrange=[0, 50.0]>], :options=>{:yrange=>[0, 100]}}>>
In [18]:
bar_graph2_per.chart.x_label 'Categories'
bar_graph2_per.chart.y_label 'Percentage (%)'
bar_graph2_per.show_in_iruby

3. Fraction

In [19]:
bar_graph2_frac = Daru::View::Plot.new(dv, type: :bar, method: :fraction)
Out[19]:
#<Daru::View::Plot:0x007fd05ca92c28 @data=#<Daru::Vector(20):cataegory>
   0 III
   1 III
   2 III
   3 III
   4 III
   5 III
   6 III
   7 III
   8 III
   9 III
  10  II
  11  II
  12  II
  13  II
  14  II
 ... ..., @options={:type=>:bar, :method=>:fraction}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fd05ca92bb0 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fd05ca91850 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"017196d6-46a0-47c8-a234-bb2c67fa8c0c"}, @xrange=["I", "II", "III"], @yrange=[0, 0.5]>], :options=>{:yrange=>[0, 1]}}>>
In [20]:
# bar_graph2_frac.chart.x_label 'Categories'
bar_graph2_frac.chart.y_label 'Fraction'
bar_graph2_frac.show_in_iruby

Categorical data visualization in Dataframe

Bar Graph

In [21]:
df = Daru::DataFrame.new({
  a: [1, 2, 4, -2, 5, 23, 0],
  b: [3, 1, 3, -6, 2, 1, 0],
  c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']
  })
df.to_category :c
df[:c].type
Out[21]:
:category
In [22]:
bar_graph3 = Daru::View::Plot.new(df, type: :bar, x: :c)
Out[22]:
#<Daru::View::Plot:0x007fd05bb12140 @data=#<Daru::DataFrame(7x3)>
       a   b   c
   0   1   3   I
   1   2   1  II
   2   4   3   I
   3  -2  -6 III
   4   5   2   I
   5  23   1 III
   6   0   0  II, @options={:type=>:bar, :x=>:c}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fd05bb12028 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fd05bb01cc8 @properties={:type=>:bar, :options=>{:value=>:c}, :data=>"dc767bc5-1edb-4065-a450-a9a3f4241eac"}, @xrange=["I", "II", "III"], @yrange=[0, 7]>], :options=>{}}>>
In [23]:
bar_graph3.show_in_iruby

Scatter plot categorized by categorical variable¶

Plots can be categorized by

  • Color
  • Size
  • Shape
In [24]:
df = Daru::DataFrame.new({
  a: [1, 2, 4, -2, 5, 23, 0],
  b: [3, 1, 3, -6, 2, 1, 0],
  c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']
  })
df.to_category :c
df[:c].type
Out[24]:
:category
In [25]:
scatter_1 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color})
Out[25]:
#<Daru::View::Plot:0x007fd05bac9af8 @data=#<Daru::DataFrame(7x3)>
       a   b   c
   0   1   3   I
   1   2   1  II
   2   4   3   I
   3  -2  -6 III
   4   5   2   I
   5  23   1 III
   6   0   0  II, @options={:type=>:scatter, :x=>:a, :y=>:b, :categorized=>{:by=>:c, :method=>:color}}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fd05bac9a80 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fd05bac3720 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"I", :color=>"rgb(102,194,165)", :tooltip_contents=>["I", "I", "I"]}, :data=>"65223cdc-8436-4095-bdc5-b7fa8f573b07"}, @xrange=[1, 5], @yrange=[2, 3]>, #<Nyaplot::Diagram:0x007fd05b24a198 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"II", :color=>"rgb(252,141,98)", :tooltip_contents=>["II", "II"]}, :data=>"b0f52746-cec0-4ef7-8a5b-10e1ea06e36f"}, @xrange=[0, 2], @yrange=[0, 1]>, #<Nyaplot::Diagram:0x007fd05c24ba10 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"III", :color=>"rgb(141,160,203)", :tooltip_contents=>["III", "III"]}, :data=>"e394ca43-e59f-4965-b25d-c42721aab2bf"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true}}>>
In [26]:
scatter_1.show_in_iruby
In [27]:
scatter_1.chart.xrange [-10, 10]
scatter_1.chart.yrange [-10, 10]
Out[27]:
In [28]:
scatter_2 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :shape})
scatter_2.show_in_iruby
In [29]:
scatter_2.chart.xrange [-10, 10]
scatter_2.chart.yrange [-10, 10]
Out[29]:
In [30]:
scatter_3 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color, color: [:red, :blue, :green]})
scatter_3.show_in_iruby
scatter_2.chart.xrange [-10, 10]
scatter_2.chart.yrange [-10, 10]
Out[30]:
In [31]:
scatter_4 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :size, size: [300, 600, 900]})
scatter_4.show_in_iruby
scatter_4.chart.xrange [-10, 10]
scatter_4.chart.yrange [-10, 10]
Out[31]:

Line plot categorized by categorical variable

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.

In [32]:
df = Daru::DataFrame.new({
  a: [1, 2, 3, 4, 5, 6, 7, 8, 9],
  b: [2, 4, 6, 1, 3, 5, 6, 4, 3],
  c: ['I']*3 + ['II']*3 + ['III']*3
  })
df.to_category :c
df[:c].type
Out[32]:
:category
In [33]:
line_1 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :color})
line_1.show_in_iruby
In [34]:
line_2 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :stroke_width})
line_2.show_in_iruby
line_2.chart.xrange [-10, 10]
line_2.chart.yrange [-10, 10]
Out[34]: