Remove duplicate legend entries in PlotlyJS combined bar plot

I would need the .mat file to verify, but I believe you could do add something under

like
legend_on = [true, false]
then change this line

to

figs[i] = PlotlyJS.plot(long_df, kind="bar", x=:date, y=:share, color=:shock, Layout(title=labels[i],
        barmode="stack", showlegend=legend_on[i]))

Edit: actually, it’s somewhat more complicated…
Here is an MWE

using PlotlyJS
using Dates
using DataFrames

startyear = 2000
time = range(Date(startyear, 1, 1), step=Dates.Quarter(1), stop=Date(2021, 12, 31))
# x = matopen("hist_dec.mat")
# hist_dec = read(x, "hist_dec")
hist_dec = rand(-0.1:0.01:0.1, 2,length(time),8);

##
labels = ["Output" "Solow residual"]
legend_on = [true, false]
# figs = [PlotlyJS.plot() for _ in 1:2]
figs = Dict()

p = make_subplots(
    rows=2,
    cols=1,
    subplot_titles=labels,
    specs= [
        Spec(kind="bar") missing
        Spec(kind="bar") missing
    ]
);

for i in 1:2
    hist_dec_i = hist_dec[i, :, :]
    # Transpose and drop measurement errors
    # hist_dec_i = hist_dec_i'
    # hist_dec_i = convert(DataFrames.DataFrame, hist_dec_i)
    hist_dec_i = DataFrame(hist_dec_i, :auto)
    hist_dec_i = hist_dec_i[!, 1:7]
    rename!(hist_dec_i, [:Consumption_tech, :Investment_tech, :Entry_tech, 
    :Intratemporal_preference, :Shopping_disutility, :Discount_factor, :Labor_supply])
    hist_dec_i[!, :date] = time
    long_df = DataFrames.stack(hist_dec_i, Not([:date]), variable_name=:shock, value_name=:share)
    figs[i] = Plot(long_df, Layout(title=labels[i],barmode="stack"); 
                    kind="bar", x=:date, y=:share, color=:shock)

    for tr in figs[i].data
        tr.fields[:showlegend] = legend_on[i]
        add_trace!(p, tr, row=i, col=1)
    end
end

relayout!(p, title_text="Historical decomposition", barmode="stack", color="shock", yaxis_title="Share", xaxis_title="Date")
display(p)
1 Like