根据@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)))