Bell Curve Overlay (Gadfly)

I have data that I’ve plotted using Geom.histogram. I’m trying to determine, visually, how closely it fits a normal distribution. I realize there are other more quantitative methods to test fit to a distribution, but for now I’m interested in the visual. How do I overlay a bell curve over my histogram? How do I set the parameters for that bell curve?
Thank you.

There’s an example in the docs, and here’s another:

using DataFrames, Distributions
D2 = [DataFrame(x=rand(Normal(μ,1), 500), μ="$(μ)") for μ in [-1, 1]]
alphs, cs = repeat([0.75, 0.85], outer=40), fill(colorant"black",2)

p2 = plot(vcat(D2...), x=:x,  color=:μ, alpha=[alphs;alphs],
    layer([x->pdf(Normal(u, 1), x) for u in [-1,1]], -4, 4, color=cs),
    Geom.histogram(position=:identity, bincount=40, limits=(min=-4, max=4),
        density=true),
    Scale.color_discrete_manual("skyblue","moccasin")
)

hist+density

That plot has some new features (a new Gadfly release is is imminent), but you can have them now by doing ]add Gadfly#master

Thank you for your reply. I’ve been trying to recreate what I’ve seen here, but it doesn’t seem to be working. I ended up copy/pasting the example in the Gadfly docs and it throws an error:

ERROR: LoadError: MethodError: no method matching layer(::var"#21#22", ::Int64, ::Int64; color=ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}}[RGB{N0f8}(0.0,0
.0,0.0)])

I’m not sure where to go from here. Thank you.

Oh I pointed you to the development docs (which will work if you’re on Gadfly#master, see note under above figure). Here’s the example from the stable docs:

using Distributions, DataFrames
gamma = Gamma(2, 2)
Dgamma = DataFrame(x=rand(gamma, 10^4))
p4 = plot(Dgamma, Coord.cartesian(xmin=0, xmax=20),
    layer(x->pdf(gamma, x), 0, 20, Geom.line, Theme(default_color="black")),
    layer(x=:x, Geom.histogram(bincount=20, density=true, limits=(min=0,))),
    Theme(default_color="bisque") )
1 Like

Wonderful, thank you. This one works. I’ll play with it to get what I need out of it.