Categorical data visualization with GnuplotRB

In [1]:
require 'gnuplotrb'
require 'daru'
include GnuplotRB
Out[1]:
Object

Plotting categorical vector

Bar graph of frequencies

In [2]:
dv = Daru::Vector.new [:a, :a, :a, :b, :b], type: :category
Out[2]:
Daru::Vector(5)
0 a
1 a
2 a
3 b
4 b
In [3]:
Plot.new [dv.frequencies, with: 'histogram'], xrange: -1..2, yrange: 0..10
Out[3]:
Gnuplot Produced by GNUPLOT 5.0 patchlevel 3 0 2 4 6 8 10 a b gnuplot_plot_1

Bar graph of percentage

In [4]:
Plot.new [dv.frequencies(:percentage), with: 'histogram'], xrange: -1..2, yrange: 0..100
Out[4]:
Gnuplot Produced by GNUPLOT 5.0 patchlevel 3 0 20 40 60 80 100 a b gnuplot_plot_1

Plotting dataframes with categorical data

In [5]:
df = Daru::DataFrame.new({
  x: [1, 2, 3, 4, 5, 6, 7, 8],
  y: [3, 2, 3, 5, 6, 1, 2, 3],
  cat: ['III', 'II', 'I', 'I', 'II', 'I', 'III', 'III']
})
df[:cat] = df[:cat].to_category categories: ['I', 'II', 'III']
df[:cat].type
Out[5]:
:category
In [6]:
dfs = df.split_by_category :cat
# The line below is because of this issue: https://github.com/SciRuby/gnuplotrb/issues/8
dfs.each { |df| df.index = df.nrows.times.to_a }
###
Plot.new(
  *dfs.map { |df| [df, using: 'x:y'] }, xrange: 0..10, yrange: 0..10
)
Out[6]:
Gnuplot Produced by GNUPLOT 5.0 patchlevel 3 0 2 4 6 8 10 0 2 4 6 8 10 I I II II III III
In [ ]:

In [ ]:

In [ ]: