Creating Boxplots with daru and statsample

Description

This example illustrates how daru, combined with Statsample::Graph::Boxplot can be used for generating box plots of a normally distributed set of data.

The 'rnorm' function, defined in statsample/shorthands generates a Daru::Vector object which contains the specified number of random variables in a normal distribution. It uses the 'distribution' gem for this purpose.

In [1]:
require 'daru'
require 'statsample'
include Statsample::Shorthand

n = 30
a = rnorm(n-1,50,10)
b = rnorm(n, 30,5)
c = rnorm(n,5,1)

# Append 2 to vector 'a' at the end
a.push(2, a.size)

a.class
Out[1]:
Daru::Vector
In [2]:
a.plot

Create a boxplot of the data by specifying the vectors a, b and c and providing necessary options to Statsample::Graph::Boxplot.

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.

In [3]:
IRuby.svg boxplot(:vectors=>[a,b,c],:width=>300, :height=>300, :groups=>%w{first first second}, :minimum=>0).to_svg
Out[3]:
0 10 20 30 40 50 60 70

IRuby.svg boxplot(:vectors=>[a,b,c],:width=>300, :height=>300, :groups=>%w{first first second}, :minimum=>0).to_svg