Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom statistics #894

Open
Mattriks opened this issue Sep 12, 2016 · 4 comments
Open

Custom statistics #894

Mattriks opened this issue Sep 12, 2016 · 4 comments

Comments

@Mattriks
Copy link
Member

Does Gadfly support custom statistics?

@Mattriks
Copy link
Member Author

Yes!:

# This uses davidavdav/CHull.jl
using DataFrames, CHull

type ChullStatistic <: Gadfly.StatisticElement
end

function Gadfly.input_aesthetics(stat::ChullStatistic)
    return [:x, :y]
end

function Gadfly.output_aesthetics(stat::ChullStatistic)
    return [:x, :y]
end

function Gadfly.default_scales(stat::ChullStatistic)
    return [Gadfly.Scale.x_continuous(), Gadfly.Scale.y_continuous()]
end


function Gadfly.Stat.apply_statistic(stat::ChullStatistic,
                         scales::Dict{Symbol, Gadfly.ScaleElement},
                         coord::Gadfly.CoordinateElement,
                         aes::Gadfly.Aesthetics)
    xy = hcat(aes.x, aes.y)
    p = chull(xy)
    hull = p.points[p.vertices,:]
    hull = vcat(hull, hull[1,:]')

    aes.x = hull[:,1]
    aes.y = hull[:,2]
end


Stat_chull = ChullStatistic

D = DataFrame(x=randn(50), y=randn(50))
layer1 = layer(Geom.point)
layer2 = layer(Stat_chull, Geom.path)
p = plot(D, x=:x, y=:y, layer1, layer2)

chull

@Mattriks
Copy link
Member Author

I think it would be good to add a page to the Gadfly documentation titled "Custom statistics and custom geometries", which includes examples as above.

@tlnagy
Copy link
Member

tlnagy commented Sep 12, 2016

That sounds like a fantastic idea. The only thing I don't like about the given example is that it has another dependency. I don't have to time to create the example myself, but if you want to go for it, I'll gladly review it and merge it.

@tlnagy
Copy link
Member

tlnagy commented Sep 13, 2016

Do you think it would be possible to update this to not use an outside dependency? I don't want the Gadfly docs to get too hard to build, that's the only reason.

I think it would be great to have this in the docs. If you need help with the new style of docs, Documenter.jl has some pretty decent examples: https://juliadocs.github.io/Documenter.jl/latest/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants