How to set labels for eached coloured bar in Plots.jl's bar()?


#1
bar(
    ["a","b","c"],
    [1,2,3],
    fillcolor = categorical(["a","b","c"]).refs,
    labels = ["a","b","c"]
)

I can make the plot with the above code, but I can’t seem to work out how to make the labels follow the same colours and text. I tried doing a group= but the bar_position = :dodge isn’t implemented.


#2

Have you tried groupedbar instead?


#3

Yes. It’s causing an error see https://github.com/JuliaPlots/StatPlots.jl/issues/129


#4

In principle it should be possible to just pass each bar as a separate series:

bar(
    ["a" "b" "c"],
    [1 2 3],
    labels = ["a" "b" "c"]
)

I say “in principle” because there appears to be a problem with passing just one series at a time.
So the way you’re doing it should be the best workaround. The only thing that doesn’t seem to work is the legend, which you want to say the exact same thing as the x labels. Could I ask you why? When would you do that?


#5

I filed the issue: the problem is that the series with only one bar (that one would get by grouping) doesn’t work.

I find the legend generally not very useful when working with bar plots (the information is generally on the axis already) but it helped us uncover a few things to fix in Plots. I’ve opened an umbrella issue at Plots about horizontal layout.


#6

Actually in my benchmarks I want those from Julia to be of a different colour to those from R


#7
bar(
    ["a" "b" "c"],
    [1 2 3],
    labels = ["a" "b" "c"],
    bar_width = 1
)

is already possible. https://github.com/JuliaPlots/Plots.jl/pull/1414 allows

bar(
    ["a" "b" "c"],
    [1 2 3],
    labels = ["a" "b" "c"],
)

as well.


#8

Great @piever!
@xiaodai for sure, I was just wondering why you’d want to duplicate that information in the legend.


#9

Awesome @daschw :slight_smile: Nice timing on the comment…


#10

Good point, hadn’t thought about helping Plots find the default bar_width. Nice job with the fix!