2

我想扩展 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

4

2 回答 2

1

根据@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)))
于 2020-04-06T06:17:18.803 回答
1

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

于 2020-04-06T00:35:06.390 回答