简体   繁体   English

在 Gadfly 中处理统计数据

[英]Processing statistics in Gadfly

I want to extend the Gadfly package to match my own idiosyncratic preferences.我想扩展 Gadfly package 以匹配我自己的特殊偏好。 However I am having trouble understanding how to use Gadfly's statistics in a way that allows for their output to be processed before plotting.但是,我无法理解如何使用 Gadfly 的统计数据,以便在绘图之前处理他们的 output。

For example, say I want to use the x,y aesthetics produced by Stat.histogram.例如,假设我想使用 Stat.histogram 产生的 x,y 美学。 To add these to a plot, I understand I can include Stat.histogram as an argument in a layer() .要将这些添加到 plot,我知道我可以将Stat.histogram作为参数包含在layer()中。 But what do I do if I want to use Stat.histogram to calculate the x,y aesthetics, edit them using my own code, and then plot these edited aesthetics?但是,如果我想使用 Stat.histogram 计算 x,y 美学,使用我自己的代码对其进行编辑,然后 plot 这些编辑后的美学该怎么办?

I'm looking for a function like load_aesthetics(layer(x=x, Stat.histogram)) , or a field like layer(x=x, Stat.histogram).aesthetics .我正在寻找像load_aesthetics(layer(x=x, Stat.histogram))这样的 function 或像layer(x=x, Stat.histogram).aesthetics的字段。

you can create your own statistic.您可以创建自己的统计数据。 see https://github.com/GiovineItalia/Gadfly.jl/issues/894https://github.com/GiovineItalia/Gadfly.jl/issues/894

Building off @bjarthur's answer, I wrote the below function.根据@bjarthur 的回答,我写了下面的 function。

"Return the aesthetics produced by a Gadfly Statistic object."
function process_statistic(statistic::Gadfly.StatisticElement,
                           input_aesthetics::Dict{Symbol,<:Any}
                           )

    # Check that enough statistics have been provided.
    required_aesthetics = Gadfly.input_aesthetics(statistic)
    for required_aesthetic in required_aesthetics
        if required_aesthetic ∉ keys(input_aesthetics) 
            error("Aesthetic $(required_aesthetic) is required")
        end
    end

    # Create the aes object, which contains the statistics.
    aes = Gadfly.Aesthetics()
    [setfield!(aes, key, value) for (key, value) in input_aesthetics]

    # These need to be passed to the apply_statistic() function. I do
    # not understand them, and the below code might need to be edited
    # for this function to work in some cases.
    scales = Dict{Symbol, Gadfly.ScaleElement}()
    coord  = Gadfly.Coord.Cartesian()

    # This function edits the aes object, filling it with the desired aesthetics.
    Gadfly.Stat.apply_statistic(statistic, scales, coord, aes)

    # Return the produced aesthetics in a dictionary.
    outputs = Gadfly.output_aesthetics(statistic)
    return Dict(output => getfield(aes, output) for output in outputs)

end

Example usage:示例用法:

process_statistic(Stat.histogram(), Dict(:x => rand(100)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM