Class: Statsample::Graph::Scatterplot

Inherits:
Object
  • Object
show all
Includes:
Summarizable
Defined in:
lib/statsample/graph/scatterplot.rb

Overview

Scatterplot

From Wikipedia: A scatter plot or scattergraph is a type of mathematical diagram using Cartesian coordinates to display values for two variables for a set of data.

The data is displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis. This kind of plot is also called a scatter chart, scatter diagram and scatter graph.

Usage

Svg output

a = Daru::Vector.new([1,2,3,4])
b = Daru::Vector.new([3,4,5,6])
puts Statsample::Graph::Scatterplot.new(a,b).to_svg

Using ReportBuilder

a = Daru::Vector.new([1,2,3,4])
b = Daru::Vector.new([3,4,5,6])
rb=ReportBuilder.new
rb.add(Statsample::Graph::Scatterplot.new(a,b))
rb.save_html('scatter.html')

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Constructor Details

#initialize(v1, v2, opts = Hash.new) ⇒ Scatterplot

Create a new Scatterplot. Params:

  • v1: Vector on X axis

  • v2: Vector on Y axis

  • opts: Hash of options. See attributes of Scatterplot


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/statsample/graph/scatterplot.rb', line 68

def initialize(v1,v2,opts=Hash.new)
  @v1_name,@v2_name = v1.name,v2.name
  @v1,@v2           = Statsample.only_valid_clone(v1,v2)
  opts_default={
    :name=>_("Scatterplot (%s - %s)") % [@v1_name, @v2_name],
    :width=>400,
    :height=>300,
    :dot_alpha=>0.5,
    :line_median=>false,
    :margin_top=>10,
    :margin_bottom=>20,
    :margin_left=>20,
    :margin_right=>20,
    :minimum_x=>nil,
    :maximum_x=>nil,
    :minimum_y=>nil,
    :maximum_y=>nil,
    :groups=>nil
  }
  @opts=opts_default.merge(opts)
  opts_default.keys.each {|k| send("#{k}=", @opts[k]) }
  @data=[]
  @v1.each_with_index {|d1,i|
    @data.push({:x=>d1, :y=>@v2[i]})
  }
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


42
43
44
# File 'lib/statsample/graph/scatterplot.rb', line 42

def data
  @data
end

#dot_alphaObject

Returns the value of attribute dot_alpha


30
31
32
# File 'lib/statsample/graph/scatterplot.rb', line 30

def dot_alpha
  @dot_alpha
end

#groupsObject

Array with assignation to groups of bars For example, for four vectors,

boxplot.groups=[1,2,1,3]

Assign same color to first and third element, and different to second and fourth


50
51
52
# File 'lib/statsample/graph/scatterplot.rb', line 50

def groups
  @groups
end

#heightObject

Total height of Scatterplot


29
30
31
# File 'lib/statsample/graph/scatterplot.rb', line 29

def height
  @height
end

#line_medianObject

Add a line on median of x and y axis


32
33
34
# File 'lib/statsample/graph/scatterplot.rb', line 32

def line_median
  @line_median
end

#margin_bottomObject

Bottom margin


36
37
38
# File 'lib/statsample/graph/scatterplot.rb', line 36

def margin_bottom
  @margin_bottom
end

#margin_leftObject

Left margin


38
39
40
# File 'lib/statsample/graph/scatterplot.rb', line 38

def margin_left
  @margin_left
end

#margin_rightObject

Right margin


40
41
42
# File 'lib/statsample/graph/scatterplot.rb', line 40

def margin_right
  @margin_right
end

#margin_topObject

Top margin


34
35
36
# File 'lib/statsample/graph/scatterplot.rb', line 34

def margin_top
  @margin_top
end

#maximum_xObject

Maximum value on x axis. Calculated automaticly from data if not set


57
58
59
# File 'lib/statsample/graph/scatterplot.rb', line 57

def maximum_x
  @maximum_x
end

#maximum_yObject

Maximum value on y axis. Calculated automaticly from data if not set.


61
62
63
# File 'lib/statsample/graph/scatterplot.rb', line 61

def maximum_y
  @maximum_y
end

#minimum_xObject

Minimum value on x axis. Calculated automaticly from data if not set


55
56
57
# File 'lib/statsample/graph/scatterplot.rb', line 55

def minimum_x
  @minimum_x
end

#minimum_yObject

Minimum value on y axis. Set to 0 if not set


59
60
61
# File 'lib/statsample/graph/scatterplot.rb', line 59

def minimum_y
  @minimum_y
end

#nameObject

Returns the value of attribute name


25
26
27
# File 'lib/statsample/graph/scatterplot.rb', line 25

def name
  @name
end

#v1Object (readonly)

Returns the value of attribute v1


43
44
45
# File 'lib/statsample/graph/scatterplot.rb', line 43

def v1
  @v1
end

#v2Object (readonly)

Returns the value of attribute v2


43
44
45
# File 'lib/statsample/graph/scatterplot.rb', line 43

def v2
  @v2
end

#widthObject

Total width of Scatterplot


27
28
29
# File 'lib/statsample/graph/scatterplot.rb', line 27

def width
  @width
end

#x_scaleObject (readonly)

Returns the value of attribute x_scale


53
54
55
# File 'lib/statsample/graph/scatterplot.rb', line 53

def x_scale
  @x_scale
end

#y_scaleObject (readonly)

Returns the value of attribute y_scale


53
54
55
# File 'lib/statsample/graph/scatterplot.rb', line 53

def y_scale
  @y_scale
end

Instance Method Details

#add_line_median(vis) ⇒ Object

Add a rule on median of X and Y axis


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/statsample/graph/scatterplot.rb', line 95

def add_line_median(vis) # :nodoc:
  that=self
  x=@x_scale
  y=@y_scale
  vis.execute {
    rule do
      data [that.v1.median]
      left x
      stroke_style Rubyvis.color("#933").alpha(0.5)
      label(:anchor=>"top") do
        text x.tick_format
      end
    end
    rule do
      data [that.v2.median]
      bottom y
      stroke_style Rubyvis.color("#933").alpha(0.5)
      label(:anchor=>"right") do
        text y.tick_format
      end
    end  
  }
  
end

#report_building(builder) ⇒ Object

:nodoc:


206
207
208
209
210
# File 'lib/statsample/graph/scatterplot.rb', line 206

def report_building(builder) # :nodoc:
  builder.section(:name=>name) do |b|
    b.image(to_svg, :type=>'svg', :width=>width, :height=>height)
  end       
end

#rubyvis_panelObject

Returns a Rubyvis panel with scatterplot


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/statsample/graph/scatterplot.rb', line 120

def rubyvis_panel # :nodoc:
  that=self
  #p @v1.map {|v| v}
  
  @minimum_x||=@v1.min
  @maximum_x||=@v1.max
  @minimum_y||=@v2.min
  @maximum_y||=@v2.max
  
  colors=Rubyvis::Colors.category10
  
  margin_hor=margin_left + margin_right
  margin_vert=margin_top  + margin_bottom
  
  x=Rubyvis::Scale.linear(@minimum_x, @maximum_x).range(0, width - margin_hor)
  y=Rubyvis::Scale.linear(@minimum_y, @maximum_y).range(0, height - margin_vert)
  @x_scale=x
  @y_scale=y
  vis=Rubyvis::Panel.new do |pan| 
    pan.width  width  - margin_hor
    pan.height height - margin_vert
    pan.bottom margin_bottom
    pan.left   margin_left
    pan.right  margin_right
    pan.top    margin_top
    # X axis
    pan.rule do
      data y.ticks
      bottom y
      stroke_style {|d| d!=0 ? "#eee" : "#000"}
      label(:anchor=>'left') do
        visible {|d| d!=0 and  d < that.width}
        text y.tick_format
      end
    end
    
    # Y axis
    pan.rule do
      data x.ticks
      left x
      stroke_style {|d| d!=0 ? "#eee" : "#000"}
      label(:anchor=>'bottom') do
        visible {|d| d>0 and d < that.height}
        text x.tick_format
      end
    end
    # Add lines on median
    add_line_median(pan) if line_median

    pan.panel do
      data(that.data)
      dot do
        left   {|d| x[d[:x]]}
        bottom {|d| y[d[:y]]}
        
        fill_style {|v| 
          alpha=(that.dot_alpha-0.3<=0) ? 0.1 : that.dot_alpha-0.3
          if that.groups
            
            colors.scale(that.groups[index]).alpha(alpha)
          else
            colors.scale(0).alpha(alpha)
          end
        }
        
        stroke_style {|v|
          if that.groups
            colors.scale(that.groups[parent.index]).alpha(that.dot_alpha)
          else
            colors.scale(0).alpha(that.dot_alpha)
          end
        }
        shape_radius 2
      end
    end
  end
  vis
end

#to_svgObject

Returns SVG with scatterplot


200
201
202
203
204
# File 'lib/statsample/graph/scatterplot.rb', line 200

def to_svg
  rp = rubyvis_panel
  rp.render
  rp.to_svg
end