Re-use existing StatsPlots Recipes?

I’m trying to define a stackedhist recipes which takes bins and array{array} and uses groupedbar to display a stacked histogram, the following function works:

using StatsPlots, StatsBase
function stack_plot(args...)
    bins = args[1] 
    # the weights of bar chart are vertical vector
    weights_ary = mapreduce(x->fit(StatsBase.Histogram, x, bins).weights, hcat, args[2])
    
    groupedbar(bins[1:end-1], weights_ary, bar_position = :stack)
end

stack_plot(0:0.1:1, [randn(100), randn(100)])

However, the following recipe does not work:

using StatsPlots, StatsBase
@userplot StackedHist
@recipe function f(h::StackedHist)
    seriestype := :groupedbar
    bar_position = :stack
    bins = h.args[1] 
    # the weights of bar chart are vertical vector
    weights_ary = mapreduce(x->fit(StatsBase.Histogram, x, bins).weights, hcat, h.args[2])
    
    bins[1:end-1], weights_ary
end

###
ERROR: The backend must not support the series type Val{:groupedbar}, and there isn't a series recipe defined.

Am I doing something obvious+stupid thing here?

It’s just that groupedbar is not a series recipe and thus cannot be used as a seriestype. Instead, return

RecipesBase.recipetype(:groupedbar, bins[1:end-1], weights_ary)

from the recipe

1 Like