Closed roland-KA closed 2 months 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:
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.
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:
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
Legend
) and use the same colors as Makie chose for the bars
# create a new column in the DataFrame `subregions_cum` which maps
# regions to integers, since Makie cannot use string values
regs = unique(subregions_cum.Region)
regmap = Dict(regs[i] => i for i in 1:length(regs))
subregions_cum.RegIndex = [regmap[r] for r in subregions_cum.Region]
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.
With dim_converts the initial example can be done with barplot(Categorical(pets), likes)
.
Closing as fixed
Also AlgebraOfGraphics can do horizontal barplots now
In packages like
Plots
,Gadfly
orVegaLite
it is possible to create a barchart in (more or less) the following way: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.