Groupedbar, groupedhist, StatPlots, StatsPlots?

So, what function in what package is best for a bar chart with multiple datasets?

So far I’ve been looking at the StatPlots and StatsPlots packages and groupedbar and groupedhist. I’m not able to install the former package because of “Unsatisfiable requirements” but its groupedbar looks promising. The latter package looks fine but I haven’t found a tutorial or documentation that I can understand. The main documentation GitHub - JuliaPlots/StatsPlots.jl: Statistical plotting recipes for Plots.jl shows examples like

using RDatasets
iris = dataset("datasets", "iris")
@df iris groupedhist(:SepalLength, group = :Species, bar_position = :dodge)

which is totally foreign to me. I’m familiar with the standard Plots interface, with which you car plot regular arrays using a straightforward syntax like

p = plot()
plot!(p, xvec, yvec)
display(p)

I’m also able to use the bar function in the Plots package, but this one doesn’t seem to have a capability of plotting multiple datasets in a single bar chart.

I’m using julia Version 1.8.2 (2022-09-29) through the juliaup framework.

After my initial post above, I have gathered more information:

  • It seems to me that StatPlots is obsolete. Its last update was 5 years ago. That’s the reason why it cannot be installed any longer.
  • StatsPlots includes groupedbar although I haven’t found a documentation for it under StatsPlots.

Perhaps should I use groupedbar in StatsPlots and refer to the documentation under StatPlots?

It seem’s your question is lacking attention. I am currently not able to try some solution. Perhaps, in the meantime, you could provide some example plot (perhaps with some example data), so we can see which plot you exactly looking for.
In general, a grouped bar plot is standard in all the main packages. So it should be no problem to give you some advise. I will be back in a few hours, but I am sure someone else will be faster.

1 Like

This is just a macro that lets you access the columns of a data frame as vectors using a symbol equal to the column name. So it’s doing what you expect. The @df macro is convenient but not required

1 Like

Just to clarify, StatPlots and StatsPlots are the same package, its just that the package was named Statplots initially and then the name changed to StatsPlots to be consistent with other package in the Stats ecosystem.

So yes you should use StatsPlots and disregard anything related to StatPlots.

1 Like

An alternative would be Algebra of graphics

https://aog.makie.org/stable/generated/analyses/

1 Like

Thank you all for your inputs. Thanks to your advice, I’ve now been successful in producing the kind of bar charts that I need.

What I wanted to produce was similar to the groupedbar(rand(10,3), bar_position = :dodge, bar_width=0.7) example in the documentation of StatsPlots ( GitHub - JuliaPlots/StatsPlots.jl: Statistical plotting recipes for Plots.jl ). Initially, I didn’t notice this example.

Anyway, I pursed this avenue. Then, I had to deal with this bug: How to unsort groupedbar categories · Issue #437 · JuliaPlots/StatsPlots.jl · GitHub
of not being able to specify the ordering of the bars.

In any case, the workaround posted there solved the problem.

My final question is, why do we have to repeat the group names?

using StatsPlots
nams = ["G8", "G9", "G10", "G11", "G12"]
ctgs = ["W category", "A category"]
plot_ctgs = repeat(ctgs, inner = size(nams,1) ) # expand
plot_nams = repeat(nams, outer = size(ctgs,1) ) # expand
arr = rand(Float64, size(nams,1), size(ctgs,1))
#display(groupedbar(nams, arr)) # (1) fine
#display(groupedbar(nams, arr; group = plot_ctgs)) # (2) error
display(groupedbar(plot_nams, arr; group = plot_ctgs)) # (3) fine

(By the way, example (3) shows the ordering problem I mention above. The bars are ordered in the alphabetical order of the names and category names!)

Algebra of graphics

That idea looks brilliant! I’ll explore it in the future. Thanks.