Jay*_*nki 14 charts graph legend julia gadfly
我使用Julia进行财务数据处理,然后根据财务数据绘制图表.
图表的X轴我在Y轴上绘制日期(每日价格)我正在绘制股票价格,MovingAverage13和MovingAverage21
我目前正在使用DataFrames绘制数据
码-
df=DataFrame(x=dates,y1=pricesClose,y2=m13,y3=m21)
l1=layer(x="x",y="y1",Geom.line,Theme(default_color=color("blue")));
l2=layer(x="x",y="y2",Geom.line,Theme(default_color=color("red")));
l3=layer(x="x",y="y3",Geom.line,Theme(default_color=color("green")));
p=plot(df,l1,l2,l3);
draw(PNG("stock.png",6inch,3inch),p)
Run Code Online (Sandbox Code Playgroud)
我正确地获取图表,但我无法在图表中添加图例,其中显示蓝线是关闭价格红线是移动平均值13绿线是移动平均值21
我们如何在图表中添加图例?
Nic*_*ico 15
我从这个链接的评论中了解到,目前无法获得图层列表的图例.
Gadfly基于Hadley Wickhams的R的ggplot2,因此通常的模式是将数据排列到具有离散列的DataFrame中以用于标记目的.在您的情况下,这种方法看起来像:
x = 1:10
df1 = DataFrame(x=x, y=2x, label="double")
df2 = DataFrame(x=x, y=x.^2, label="square")
df3 = DataFrame(x=x, y=1./x, label="inverse")
df = vcat(df1, df2, df3)
p = plot(df, x="x", y="y", color="label", Geom.line,
Scale.discrete_color_manual("blue","red", "green"))
draw(PNG("stock.png", 6inch, 3inch), p)
Run Code Online (Sandbox Code Playgroud)

现在,您可以尝试使用manual_color_key。您的代码中唯一需要更改的地方是:
p=plot(df,l1,l2,l3,
Guide.ylabel("Some text"),
Guide.title("My title"),
Guide.manual_color_key("Legend", ["I'm blue l1", "I'm red l2", "I'm green l3"], ["blue", "red", "green"]))