我想扩展Gadfly包以匹配我自己的特殊偏好。然而,我很难理解如何使用Gadfly的统计数据,以便在绘制之前对其输出进行处理。
例如,假设我想使用Stat.histogram产生的x,y美学。要将这些添加到图中,我知道我可以在layer()中包含Stat.histogram作为参数。但是,如果我想使用Stat.histogram计算x,y美学,使用我自己的代码编辑它们,然后绘制这些编辑后的美学,我该怎么办呢?
我正在寻找像load_aesthetics(layer(x=x, Stat.histogram))这样的函数,或者像layer(x=x, Stat.histogram).aesthetics这样的字段。
发布于 2020-04-06 14:17:19
基于@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-06 08:35:06
您可以创建自己的统计数据。请参阅https://github.com/GiovineItalia/Gadfly.jl/issues/894
https://stackoverflow.com/questions/61048626
复制相似问题