I have alreay explained about Nyaplot::DataFrame in tutorial 1, but it's not enough to tell the usefulness. This notebook consists of 2 use case using DataFrame.
In [1]:
require 'nyaplot'
Out[1]:
Out[1]:
First, prepare sample data and put it into DataFrame. Then build a scatter plot based on it.
In [2]:
samples = Array.new(10).map.with_index{|d,i| 'cat'+i.to_s}
x=[];y=[];home=[]
10.times do
x.push(5*rand)
y.push(5*rand)
end
df = Nyaplot::DataFrame.new({x:x,y:y,name:samples})
df
Out[2]:
In [3]:
plot = Nyaplot::Plot.new
plot.x_label("weight [kg]")
plot.y_label("height [m]")
sc = plot.add_with_df(df, :scatter, :x, :y)
Out[3]:
In [4]:
plot.show
Out[4]:
The plot above is not contain name
information, so add it into tool-tip. Use tooltip_contents
to add contents to tool-tip.
In [5]:
sc.tooltip_contents([:name])
plot.show
Out[5]:
Tool-tip can include multiple lines, but the DataFrame has only three columns and that's not enough to add more line to tool-tip. Let's add home
column to it.
In [6]:
address = ['London', 'Kyoto', 'Los Angeles', 'Puretoria']
home = Array.new(10,'').map{|d| address.clone.sample}
df.home = home
df
Out[6]:
In [7]:
sc.tooltip_contents([:name, :home])
plot.show
Out[7]:
Then, fill points on the scatter in different colors according to 'home' column. To do so, specify column name by fill_by
method.
In [8]:
colors = Nyaplot::Colors.qual
Out[8]:
In [9]:
sc.color(colors)
sc.fill_by(:home)
plot.show
Out[9]:
Use shape_by
method to change shape according to value in a column.
In [10]:
sc.color(colors)
sc.shape_by(:home)
plot.show
Out[10]:
DataFrame is also useful when visualizing data in multiple panes. Let's create plot from data about mutation.
First, fetch data from csv file. (All data used in this Tutorial is included in Nyaplot's repository: /examples/notebook/data/*)
In [11]:
path = File.expand_path("../data/first.tab", __FILE__)
df = Nyaplot::DataFrame.from_csv(path, sep="\t")
Out[11]:
Now I want to plot SET1 column, but it contains many zero cells. Then filter them out.
In [12]:
df.filter! {|row| row[:set1] != 0.0}
df
Out[12]:
Next prepare instance of Nyaplot::Plot as usual. Nyaplot::Plot.filter is a method for adding 'filter box' to the plot.
In [13]:
plot4=Nyaplot::Plot.new
plot4.add_with_df(df, :histogram, :set1)
plot4.configure do
height(400)
x_label('PNR')
y_label('Frequency')
filter({target:'x'})
yrange([0,130])
end
plot5=Nyaplot::Plot.new
plot5.add_with_df(df, :bar, :mutation)
plot5.configure do
height(400)
x_label('Mutation types')
y_label('Frequency')
yrange([0,100])
end
Out[13]:
Then create an instance of Nyaplot::Frame. It can hold multiple plots in it, and it helps them to interact with each other.
In [14]:
frame = Nyaplot::Frame.new
frame.add(plot4)
frame.add(plot5)
frame.show
Out[14]: