In [1]:
require 'daru/view'


Out[1]:
true

In [2]:
Daru::View.plotting_library = :nyaplot


Out[2]:
:nyaplot

Categorical Vector Visualization


In [3]:
dv = Daru::Vector.new [:a, :a, :a, :b, :b, :c], type: :category


Out[3]:
Daru::Vector(6):category
0 a
1 a
2 a
3 b
4 b
5 c

Bar graph

1. Frequency (count)

In [4]:
bar_graph1 = Daru::View::Plot.new(dv, type: :bar)


Out[4]:
#<Daru::View::Plot:0x0000563f08adfd20 @data=#<Daru::Vector(6):category>
   0   a
   1   a
   2   a
   3   b
   4   b
   5   c, @options={:type=>:bar}, @user_options={}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x0000563f08adf4b0 @properties={:diagrams=>[#<Nyaplot::Diagram:0x0000563f08ac47c8 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"0d4ca097-306d-4eca-8587-dbe9b7216db5"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{}}>>

In [5]:
bar_graph1.chart.class


Out[5]:
Nyaplot::Plot

In [6]:
bar_graph1.show_in_iruby



In [7]:
dv = Daru::Vector.new ['III']*10 + ['II']*5 + ['I']*5, type: :category, categories: ['I', 'II', 'III']
dv.type


Out[7]:
:category

In [8]:
bar_graph2 = Daru::View::Plot.new(dv, type: :bar)


Out[8]:
#<Daru::View::Plot:0x0000563f09674780 @data=#<Daru::Vector(20):category>
   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}, @user_options={}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x0000563f096743e8 @properties={:diagrams=>[#<Nyaplot::Diagram:0x0000563f0966edd0 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"aaff240c-c071-4d9c-a797-2e2a0d2e4acc"}, @xrange=["I", "II", "III"], @yrange=[0, 10]>], :options=>{}}>>

In [9]:
bar_graph2.class


Out[9]:
Daru::View::Plot

In [10]:
bar_graph2.chart.class


Out[10]:
Nyaplot::Plot

In [11]:
# Nyaplot methods will work.Since #chart is Nyaplot::Plot class object
bar_graph2.chart.x_label 'Cat'
bar_graph2.chart.y_label 'Frequency'


Out[11]:

In [12]:
bar_graph2.chart # or bar_graph2.show_in_iruby


Out[12]:

2. Percentage


In [13]:
bar_graph2_per = Daru::View::Plot.new(dv, type: :bar, method: :percentage)


Out[13]:
#<Daru::View::Plot:0x0000563f09384db8 @data=#<Daru::Vector(20):category>
   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}, @user_options={}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x0000563f09384d18 @properties={:diagrams=>[#<Nyaplot::Diagram:0x0000563f093777a8 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"bd6240eb-4cf9-4ac0-ac36-b3d881417ea6"}, @xrange=["I", "II", "III"], @yrange=[0, 50.0]>], :options=>{:yrange=>[0, 100]}}>>

In [14]:
bar_graph2_per.chart.x_label 'Categories'
bar_graph2_per.chart.y_label 'Percentage (%)'
bar_graph2_per.show_in_iruby


3. Fraction


In [15]:
bar_graph2_frac = Daru::View::Plot.new(dv, type: :bar, method: :fraction)


Out[15]:
#<Daru::View::Plot:0x0000563f08e16930 @data=#<Daru::Vector(20):category>
   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}, @user_options={}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x0000563f08e15ee0 @properties={:diagrams=>[#<Nyaplot::Diagram:0x0000563f08e09438 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"123767c9-f679-4cfb-b011-68d860975481"}, @xrange=["I", "II", "III"], @yrange=[0, 0.5]>], :options=>{:yrange=>[0, 1]}}>>

In [16]:
# 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 [17]:
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[17]:
:category

In [18]:
bar_graph3 = Daru::View::Plot.new(df, type: :bar, x: :c)


Out[18]:
#<Daru::View::Plot:0x0000563f0869fe40 @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}, @user_options={}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x0000563f08673200 @properties={:diagrams=>[#<Nyaplot::Diagram:0x0000563f0984acd0 @properties={:type=>:bar, :options=>{:value=>:c}, :data=>"c80a88de-f7ea-4863-b89d-1da3265012db"}, @xrange=["I", "II", "III"], @yrange=[0, 7]>], :options=>{}}>>

In [19]:
bar_graph3.show_in_iruby


Scatter plot categorized by categorical variable

Plots can be categorized by

  • Color
  • Size
  • Shape

In [20]:
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[20]:
:category

In [21]:
scatter_1 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color})


Out[21]:
#<Daru::View::Plot:0x0000563f096583c8 @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}}, @user_options={}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x0000563f0964bc90 @properties={:diagrams=>[#<Nyaplot::Diagram:0x0000563f0963a170 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"I", :color=>"rgb(141,211,199)", :tooltip_contents=>["I", "I", "I"]}, :data=>"11bbcb70-b57d-4ae0-83f9-06904e460b0c"}, @xrange=[1, 5], @yrange=[2, 3]>, #<Nyaplot::Diagram:0x0000563f096227f0 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"II", :color=>"rgb(255,255,179)", :tooltip_contents=>["II", "II"]}, :data=>"3c02adbf-fd35-40f6-ac50-204b752b15d9"}, @xrange=[0, 2], @yrange=[0, 1]>, #<Nyaplot::Diagram:0x0000563f09602a90 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"III", :color=>"rgb(190,186,218)", :tooltip_contents=>["III", "III"]}, :data=>"2bf57fd9-994f-45ef-b957-e0638ab2b6a5"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true}}>>

In [22]:
scatter_1.show_in_iruby



In [23]:
scatter_1.chart.xrange [-10, 10]
scatter_1.chart.yrange [-10, 10]


Out[23]:

In [24]:
scatter_2 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :shape})
scatter_2.show_in_iruby



In [25]:
scatter_2.chart.xrange [-10, 10]
scatter_2.chart.yrange [-10, 10]


Out[25]:

In [26]:
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[26]:

In [27]:
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[27]:

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 [28]:
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[28]:
:category

In [29]:
line_1 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :color})
line_1.show_in_iruby



In [30]:
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[30]:

In [ ]: