Rubyvis - Example: Scatterplot

Scatterplots can facilitate visual analysis along multiple dimensions, though care should be taken to avoid interference. In this example, we encode three dimensions: two are encoded using position, while the third is redundantly encoded as both area and color.

require 'rubyvis'
data = pv.range(100).map {|x|{x: x, y: rand(), z: 10**(2*rand)})

w = 400
h = 400

x = pv.Scale.linear(0, 99).range(0, w)
y = pv.Scale.linear(0, 1).range(0, h)

c = pv.Scale.log(1, 100).range("orange", "brown")

# The root panel.
vis =

# Y-axis and ticks. 
    .strokeStyle(lambda {|d| d!=0 ? "#eee" : "#000"})
  .visible(lambda {|d|  d > 0 and d < 1})

# X-axis and ticks. 
    .stroke_style(lambda {|d| d!=0 ? "#eee" : "#000"})
  .visible(lambda {|d|  d > 0 and d < 100})

#/* The dot plot! */
  .left(lambda {|d| x.scale(d.x)})
  .bottom(lambda {|d| y.scale(d.y)})
  .stroke_style(lambda {|d| c.scale(d.z)})
  .fill_style(lambda {|d| c.scale(d.z).alpha(0.2)})
  .shape_size(lambda {|d| d.z})
  .title(lambda {|d| "%0.1f" % d.z})

puts vis.to_svg