Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
150 lines (119 sloc) 4.57 KB

Guides

Guide.annotation

using Gadfly, Compose
set_default_plot_size(14cm, 8cm)
plot(sin, 0, 2pi, Guide.annotation(compose(context(),
     Shape.circle([pi/2, 3*pi/2], [1.0, -1.0], [2mm]),
     fill(nothing), stroke("orange"))))

Guide.colorkey

using Gadfly, RDatasets
set_default_plot_size(14cm, 8cm)
Dsleep = dataset("ggplot2", "msleep")[:,[:Vore,:BrainWt,:BodyWt,:SleepTotal]]
DataFrames.dropmissing!(Dsleep)
Dsleep.SleepTime = Dsleep.SleepTotal .> 8
plot(Dsleep, x=:BodyWt, y=:BrainWt, Geom.point, color=:SleepTime, 
     Guide.colorkey(title="Sleep", labels=[">8","≤8"]),
     Scale.x_log10, Scale.y_log10 )
using Gadfly, Compose, RDatasets
set_default_plot_size(21cm, 8cm)
iris = dataset("datasets","iris")
pa = plot(iris, x=:SepalLength, y=:PetalLength, color=:Species, Geom.point,
          Theme(key_position=:inside) )
pb = plot(iris, x=:SepalLength, y=:PetalLength, color=:Species, Geom.point, 
          Guide.colorkey(title="Iris", pos=[0.05w,-0.28h]) )
hstack(pa, pb)

Guide.manual_color_key, Guide.manual_discrete_key

using DataFrames, Gadfly, RDatasets
set_default_plot_size(21cm, 7cm)
points = DataFrame(index=rand(0:10,30), val=rand(1:10,30))
line = DataFrame(val=rand(1:10,11), index=collect(0:10))
p1 = plot(layer(points, x=:index, y=:val, color=[colorant"green"]),
    layer(line, x=:index, y=:val, Geom.line),
    Guide.manual_color_key("Legend", ["Points", "Line"], ["green", "deepskyblue"],
        shape=[Shape.circle, Shape.hline]))

D = groupby(dataset("COUNT", "titanicgrp"), :Class)
fn1(s,c) = 100*s./sum(c)
D = combine(D, :Age, :Sex, [:Survive, :Cases]=>fn1=>:prcnt)
p2 = plot(stack(D, [:Age, :Sex]), xgroup=:Class,
    Geom.subplot_grid(layer(x=:variable, y=:prcnt, color=:value, Geom.bar)),
    Scale.x_discrete, Guide.ylabel("Survival (% Class)"),
    Guide.manual_color_key("Age", ["children","adults"], 1:2),
    Guide.manual_color_key("Sex", ["female","male"], 3:4),
    Theme(bar_spacing=1mm, key_position=:none,
        key_swatch_shape=Shape.square, point_size=4pt))
hstack(p1, p2)

Guide.shapekey

using Compose, Gadfly, RDatasets
set_default_plot_size(16cm, 8cm)
Dsleep = dataset("ggplot2", "msleep")
Dsleep = dropmissing!(Dsleep[:,[:Vore, :Name,:BrainWt,:BodyWt, :SleepTotal]])
Dsleep.SleepTime = Dsleep.SleepTotal .> 8
plot(Dsleep, x=:BodyWt, y=:BrainWt, Geom.point, color=:Vore, shape=:SleepTime,
    Scale.x_log10, Scale.y_log10, Guide.colorkey,
    Guide.shapekey(title="Sleep (hrs)", labels=[">8","≤8"]),
    Theme(point_size=2mm, key_swatch_color="slategrey", 
            point_shapes=[Shape.utriangle, Shape.dtriangle]) )

Guide.sizekey

using Compose, Gadfly, RDatasets
set_default_plot_size(14cm, 8cm)

D = groupby(dataset("datasets", "Titanic"), :Class)
Titanic = combine(D, :, :Freq=>(c->100*c./sum(c))=>:prcnt)
filter!(:Survived=>x->x=="Yes", Titanic)
sizemap = n->range(3pt, 8pt, length=n)

plot(Titanic, Scale.x_log10,  Scale.y_log10,
    x=:Freq, y=:prcnt, color=:Age, shape=:Sex,  size=:Class,
    Scale.size_discrete2(sizemap), Guide.sizekey(title="Passenger\n Class"),
    Guide.colorkey(pos=[0.1, -0.3h]), Guide.shapekey(pos=[0.5, -0.31h]),
    Guide.ylabel("% of Passenger Class"),
 Theme(discrete_highlight_color=identity, alphas=[0.1], key_swatch_color="grey",
    key_swatch_shape=Shape.circle, point_size=3pt) )

Guide.title

using Gadfly, RDatasets
set_default_plot_size(14cm, 8cm)
plot(dataset("ggplot2", "diamonds"), x="Price", Geom.histogram,
     Guide.title("Diamond Price Distribution"))

Guide.xlabel, Guide.ylabel

using Gadfly
set_default_plot_size(21cm, 8cm)
p1 = plot(cos, 0, 2π, Guide.xlabel("Angle"));
p2 = plot(cos, 0, 2π, Guide.xlabel("Angle", orientation=:vertical));
p3 = plot(cos, 0, 2π, Guide.xlabel(nothing));
hstack(p1,p2,p3)

Guide.xrug, Guide.yrug

using Gadfly
set_default_plot_size(14cm, 8cm)
plot(x=rand(20), y=rand(20), Guide.xrug)

Guide.xticks, Guide.yticks

using Gadfly
set_default_plot_size(21cm, 8cm)
ticks = [0.1, 0.3, 0.5]
p1 = plot(x=rand(10), y=rand(10), Geom.line, Guide.xticks(ticks=ticks))
p2 = plot(x=rand(10), y=rand(10), Geom.line, Guide.xticks(ticks=ticks, label=false))
p3 = plot(x=rand(10), y=rand(10), Geom.line,
          Guide.xticks(ticks=ticks, orientation=:vertical))
hstack(p1,p2,p3)
using Gadfly
set_default_plot_size(14cm, 8cm)
plot(x=rand(1:10, 10), y=rand(1:10, 10), Geom.line, Guide.xticks(ticks=1:9))