How to overplot multiple arrays of data in one Gadfly plot?

3.6k views Asked by At

I'm using Gadfly to plot data in Julia. I have x = an array of floats, and several y1, y2, y3 ... of matching length. How to I plot all the points (x,y1) in green, (x,y2) in red, etc. in one Gadfly plot?

2

There are 2 answers

2
Vincent Zoonekynd On

You can put the data in a DataFrame, with three columns, x, y and group, and use the group as a colour aesthetic.

# Sample data
n  = 10
x  = collect(1:n)
y1 = rand(n)
y2 = rand(n)
y3 = rand(n)

# Put the data in a DataFrame
using DataFrames
d = DataFrame( 
  x = vcat(x,x,x),
  y = vcat(y1,y2,y3),
  group = vcat( rep("1",n), rep("2",n), rep("3",n) )
)

# Plot
using Gadfly
plot( 
  d, 
  x=:x, y=:y, color=:group, 
  Geom.point,
  Scale.discrete_color_manual("green","red","blue")
)

Plot

As suggested in the comments, you could also use layers:

plot(
  layer(x=x, y=y1, Geom.point, Theme(default_color=color("green"))),
  layer(x=x, y=y2, Geom.point, Theme(default_color=color("red"))),
  layer(x=x, y=y3, Geom.point, Theme(default_color=color("blue")))
)
0
Tom Breloff On

This sort of stuff is simple with my package https://github.com/tbreloff/Plots.jl:

julia> using Plots; scatter(rand(10,3), c=[:green,:red,:blue])

enter image description here