Gadfly 中的处理统计信息

Processing statistics in Gadfly

我想扩展 Gadfly 包以匹配我自己的特殊偏好。但是,我无法理解如何以允许在绘图之前处理其输出的方式使用 Gadfly 的统计信息。

例如,假设我想使用 Stat.histogram 生成的 x,y 美学。要将这些添加到绘图中,我知道我可以将 Stat.histogram 作为参数包含在 layer() 中。但是,如果我想使用 Stat.histogram 计算 x,y 美学,使用我自己的代码编辑它们,然后绘制这些编辑后的美学,我该怎么办?

我正在寻找 load_aesthetics(layer(x=x, Stat.histogram)) 之类的函数,或 layer(x=x, Stat.histogram).aesthetics.

之类的字段

您可以创建自己的统计信息。见 https://github.com/GiovineItalia/Gadfly.jl/issues/894

根据@bjarthur 的回答,我编写了以下函数。

"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

用法示例:

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