Geometries

Geometries

Geom.abline

using Gadfly, RDatasets, Compose
set_default_plot_size(14cm, 8cm)
plot(dataset("ggplot2", "mpg"),
     x="Cty", y="Hwy", label="Model", Geom.point, Geom.label,
     intercept=[0], slope=[1], Geom.abline(color="red", style=:dash),
     Guide.annotation(compose(context(), text(6,4, "y=x", hleft, vtop), fill("red"))))
Cty 0 10 20 30 40 a4 a4 a4 a4 a4 a4 a4 a4 quattro a4 quattro a4 quattro a4 quattro a4 quattro a4 quattro a4 quattro a4 quattro a6 quattro a6 quattro a6 quattro c1500 suburban 2wd c1500 suburban 2wd c1500 suburban 2wd c1500 suburban 2wd c1500 suburban 2wd corvette corvette corvette corvette corvette k1500 tahoe 4wd k1500 tahoe 4wd k1500 tahoe 4wd k1500 tahoe 4wd malibu malibu malibu malibu malibu caravan 2wd caravan 2wd caravan 2wd caravan 2wd caravan 2wd caravan 2wd caravan 2wd caravan 2wd caravan 2wd caravan 2wd caravan 2wd dakota pickup 4wd dakota pickup 4wd dakota pickup 4wd dakota pickup 4wd dakota pickup 4wd dakota pickup 4wd dakota pickup 4wd dakota pickup 4wd dakota pickup 4wd durango 4wd durango 4wd durango 4wd durango 4wd durango 4wd durango 4wd durango 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd ram 1500 pickup 4wd expedition 2wd expedition 2wd expedition 2wd explorer 4wd explorer 4wd explorer 4wd explorer 4wd explorer 4wd explorer 4wd f150 pickup 4wd f150 pickup 4wd f150 pickup 4wd f150 pickup 4wd f150 pickup 4wd f150 pickup 4wd f150 pickup 4wd mustang mustang mustang mustang mustang mustang mustang mustang mustang civic civic civic civic civic civic civic civic civic sonata sonata sonata sonata sonata sonata sonata tiburon tiburon tiburon tiburon tiburon tiburon tiburon grand cherokee 4wd grand cherokee 4wd grand cherokee 4wd grand cherokee 4wd grand cherokee 4wd grand cherokee 4wd grand cherokee 4wd grand cherokee 4wd range rover range rover range rover range rover navigator 2wd navigator 2wd navigator 2wd mountaineer 4wd mountaineer 4wd mountaineer 4wd mountaineer 4wd altima altima altima altima altima altima maxima maxima maxima pathfinder 4wd pathfinder 4wd pathfinder 4wd pathfinder 4wd grand prix grand prix grand prix grand prix grand prix forester awd forester awd forester awd forester awd forester awd forester awd impreza awd impreza awd impreza awd impreza awd impreza awd impreza awd impreza awd impreza awd 4runner 4wd 4runner 4wd 4runner 4wd 4runner 4wd 4runner 4wd 4runner 4wd camry camry camry camry camry camry camry camry solara camry solara camry solara camry solara camry solara camry solara camry solara corolla corolla corolla corolla corolla land cruiser wagon 4wd land cruiser wagon 4wd toyota tacoma 4wd toyota tacoma 4wd toyota tacoma 4wd toyota tacoma 4wd toyota tacoma 4wd toyota tacoma 4wd toyota tacoma 4wd gti gti gti gti gti jetta jetta jetta jetta jetta jetta jetta jetta jetta new beetle new beetle new beetle new beetle new beetle new beetle passat passat passat passat passat passat passat y=x 0 10 20 30 40 50 Hwy

Geom.bar

using Gadfly, RDatasets
set_default_plot_size(14cm, 8cm)
plot(dataset("HistData", "ChestSizes"), x="Chest", y="Count", Geom.bar)
Chest 30 35 40 45 50 0 500 1000 1500 Count
using Gadfly, RDatasets, DataFrames
set_default_plot_size(21cm, 8cm)

D = by(dataset("datasets","HairEyeColor"), [:Eye,:Sex], d->sum(d[:Freq]))
p1 = plot(D, color="Eye", y="x1", x="Sex", Geom.bar(position=:dodge),
          Guide.ylabel("Freq"));

rename!(D, :x1 => :Frequency)
palette = ["brown","blue","tan","green"]  # Is there a hazel color?

p2a = plot(D, x=:Sex, y=:Frequency, color=:Eye, Geom.bar(position=:stack),
           Scale.color_discrete_manual(palette...));
p2b = plot(D, x=:Sex, y=:Frequency, color=:Eye, Geom.bar(position=:stack),
           Scale.color_discrete_manual(palette[4:-1:1]..., order=[4,3,2,1]));

hstack(p1, p2a, p2b)
Sex Male Female Green Hazel Blue Brown Eye 0 100 200 300 400 Frequency Sex Male Female Brown Blue Hazel Green Eye 0 100 200 300 400 Frequency Sex Male Female Brown Blue Hazel Green Eye 0 50 100 150 Freq

See Scale.color_discrete_manual for more information.

Geom.beeswarm

using Gadfly, RDatasets
set_default_plot_size(14cm, 8cm)
plot(dataset("lattice", "singer"), x="VoicePart", y="Height", Geom.beeswarm)
VoicePart Soprano 1 Soprano 2 Alto 1 Alto 2 Tenor 1 Tenor 2 Bass 1 Bass 2 60 65 70 75 80 Height

Geom.boxplot

using Gadfly, RDatasets
set_default_plot_size(14cm, 8cm)
plot(dataset("lattice", "singer"), x="VoicePart", y="Height", Geom.boxplot)
VoicePart Soprano 1 Soprano 2 Alto 1 Alto 2 Tenor 1 Tenor 2 Bass 1 Bass 2 60 65 70 75 80 Height

Geom.contour

using Gadfly
set_default_plot_size(14cm, 8cm)
plot(z=(x,y) -> x*exp(-(x-round(Int, x))^2-y^2),
     xmin=[-8], xmax=[8], ymin=[-2], ymax=[2], Geom.contour)
xmin -10 -5 0 5 10 5 10 -10 -5 0 Color -2 -1 0 1 2 ymin
using Gadfly, RDatasets
set_default_plot_size(21cm, 16cm)
volcano = Matrix{Float64}(dataset("datasets", "volcano"))
p1 = plot(z=volcano, Geom.contour)
p2 = plot(z=volcano, Geom.contour(levels=[110.0, 150.0, 180.0, 190.0]))
p3 = plot(z=volcano, x=collect(0.0:10:860.0), y=collect(0.0:10:600.0),
          Geom.contour(levels=2))
Mv = volcano[1:4:end, 1:4:end]
Dv = vcat([DataFrame(x=[1:size(Mv,1);], y=j, z=Mv[:,j]) for j in 1:size(Mv,2)]...)
p4 = plot(Dv, x=:x, y=:y, z=:z, color=:z,
          Coord.cartesian(xmin=1, xmax=22, ymin=1, ymax=16),
          Geom.point, Geom.contour(levels=10),
          style(line_width=0.5mm, point_size=0.2mm) )
gridstack([p1 p2; p3 p4])
x 5 10 15 20 200 100 50 150 z 5 10 15 y x 0 200 400 600 800 130 160 140 150 170 120 Color 0 100 200 300 400 500 600 y 0 25 50 75 100 140 120 170 190 180 110 130 150 160 Color 0 20 40 60 80 0 25 50 75 100 200 100 150 Color 0 20 40 60 80

Geom.density

using Gadfly, RDatasets, Distributions
set_default_plot_size(21cm, 8cm)
p1 = plot(dataset("ggplot2", "diamonds"), x="Price", Geom.density)
p2 = plot(dataset("ggplot2", "diamonds"), x="Price", color="Cut", Geom.density)
hstack(p1,p2)
Price -1×10⁴ 0 1×10⁴ 2×10⁴ 3×10⁴ Ideal Premium Good Very Good Fair Cut 0.0000 0.0001 0.0002 0.0003 0.0004 0.0005 Price -1×10⁴ 0 1×10⁴ 2×10⁴ 3×10⁴ 0.0000 0.0001 0.0002 0.0003 0.0004
using Gadfly, RDatasets, Distributions
set_default_plot_size(14cm, 8cm)
dist = MixtureModel(Normal, [(0.5, 0.2), (1, 0.1)])
xs = rand(dist, 10^5)
plot(layer(x=xs, Geom.density, Theme(default_color="orange")),
     layer(x=xs, Geom.density(bandwidth=0.0003), Theme(default_color="green")),
     layer(x=xs, Geom.density(bandwidth=0.25), Theme(default_color="purple")),
     Guide.manual_color_key("bandwidth", ["auto", "bw=0.0003", "bw=0.25"],
                            ["orange", "green", "purple"]))
x -2 -1 0 1 2 3 auto bw=0.0003 bw=0.25 bandwidth 0.0 0.5 1.0 1.5 2.0 2.5

Geom.density2d

using Gadfly, Distributions
set_default_plot_size(14cm, 8cm)
plot(x=rand(Rayleigh(2),1000), y=rand(Rayleigh(2),1000),
     Geom.density2d(levels = x->maximum(x)*0.5.^collect(1:2:8)), Geom.point,
     Theme(key_position=:none),
     Scale.color_continuous(colormap=x->colorant"red"))
x -5 0 5 10 -2 0 2 4 6 8 y

Geom.ellipse

using RDatasets, Gadfly
set_default_plot_size(21cm, 8cm)
D = dataset("datasets","faithful")
D[:g] = D[:Eruptions].>3.0
coord = Coord.cartesian(ymin=35, ymax=100)
pa = plot(D, coord,
          x=:Eruptions, y=:Waiting, group=:g,
          Geom.point, Geom.ellipse)
pb = plot(D, coord,
          x=:Eruptions, y=:Waiting, color=:g,
          Geom.point, Geom.ellipse,
          layer(Geom.ellipse(levels=[0.99]), style(line_style=[:dot])),
          style(key_position=:none), Guide.ylabel(nothing))
hstack(pa,pb)
Eruptions 0 1 2 3 4 5 6