How to reconfigure multiple datasets in one Gadfly patch?

I use Gadfly to write data to Julia. I have x = an array of floats and several y1, y2, y3 ... of matching length. How to build all points (x, y1) in green (x, y2) in red, etc. In one picture of a Gadfly?

+4
source share
2 answers

You can put data in DataFrame with three columns, x, yand group, and use the group as a color aesthetics.

# 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 stated in the comments, you can 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")))
)
+6
source

https://github.com/tbreloff/Plots.jl:

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

enter image description here

0

All Articles