MakieOrg / Makie.jl

Interactive data visualizations and plotting in Julia
https://docs.makie.org/stable
MIT License
2.41k stars 312 forks source link

Barplots etc. only with numerical data? #1507

Closed roland-KA closed 2 months ago

roland-KA commented 2 years ago

In packages like Plots, Gadfly or VegaLite it is possible to create a barchart in (more or less) the following way:

pets = ["cat", "dog", "fish"]
likes = [23, 76, 26]
barplot(pets, likes)

In Makie this leads to an error, because it expects numeric data in each array. Is there really no easy way to do this in Makie or do I miss something?

I know, that it is possible using AlgebraOfGraphics, but that package has other limitations.

lazarusA commented 2 years ago

in this is example

https://github.com/lazarusA/100daysOfMakie#14-barplot-and-errorbars

is not as simple as that one, but you have a lot more freedom:

SimonDanisch commented 2 years ago

Once this is merged, default Makie should also be able to handle it: https://github.com/JuliaPlots/Makie.jl/pull/1347 Meanwhile, AlgebraOfGraphcis.jl can be used for such tasks.

roland-KA commented 2 years ago

Thanks for your help! But what I really want is a bit more complicated than the toy example I've mentioned above.

For my lecture in Data Science I've made a comparison of different Julia graphics packages for statistical plots (StatisticalPlotsWithJulia).

Among other examples, I create there the following plot, which shows the population size of different 'subregions' and uses color to show to which continent (here called: region) the subregion belongs:

image

Some of the special aspects of this example are:

In all packages I've used so far

This can be achieved in Gadfly (DV-Basics-Gadfly.jl) with these few lines of code:

subregions_cum_sorted = sort(subregions_cum, :Pop2019)
barplot5 = plot(subregions_cum_sorted, 
    x = :Pop2019, y = :Subregion, color = :Region, 
    Geom.bar(orientation = :horizontal),
    Guide.title("Population by Subregion, 2019"),
    Guide.ylabel("Subregion"),
    Guide.xlabel("Population [millions]"),
    Scale.x_continuous(format = :plain),
    Theme(background_color = "ghostwhite", bar_spacing = 1mm)   
)

An attempt to implement the example using Makie resulted in the following code, which creates the bar plot. But I'm stuck with the legend. I have no idea on how to

subregions_cum_sorted = sort(subregions_cum, :Pop2019) fig_bp99 = Figure( resolution = (1200, 600), backgroundcolor = :ghostwhite, ) subreg_idx99 = eachindex(subregions_cum_sorted.Subregion) # Makie cannot handle strings barplot99 = barplot( fig_bp99[1, 1], subreg_idx99, subregions_cum_sorted.Pop2019, colormap = :Set2_8, color = subregions_cum_sorted.RegIndex, axis = ( yticks = (subreg_idx99, subregions_cum_sorted.Subregion), title = "Population by Region, 2019", ylabel = "Subregion", xlabel = "Population [millions]" ), direction = :x ) fig_bp99



@SimonDanisch #1347 looks really promising 👍😊, I'm looking forward to it.

And I've already done most of the examples of my comparison using AlgebraOfGraphics a few months ago ([DV-Basics-AlgebraOfGraphics.jl](https://github.com/roland-KA/StatisticalPlotsWithJulia/blob/main/notebooks/DV-Basics-AlgebraOfGraphics.jl)). But even with the help of Pietro Vertechi I couldn't resolve all issues (as noted within that notebook).

In general I like the idea of AlgebraOfGraphics very much. Up to now, I've never seen such a conceptually pure and beautiful implementation of the grammar of graphics. But unfortunately it isn't mature enough at the moment.
ffreyer commented 2 months ago

With dim_converts the initial example can be done with barplot(Categorical(pets), likes). Closing as fixed

jkrumbiegel commented 2 months ago

Also AlgebraOfGraphics can do horizontal barplots now