Makie version of a simple bar graph howto

Hello,

I am trying to settle on plotting library. I looked into gadfly and very much like the syntax, but the documentation seems lacking, if I am not wrong, it lacks 3d, real time update features.

I find makie documentation through, easy to grasp, the syntax looks conventional and based on the post answer, it seems it can handle real-time plot update.

To feel more confident, I would like to be able plot the bar chart with both library by using following data.

country,percent
country1,47
country2,41
country3,39
country4,39
country5,36
country6,36
country7,36
country8,35
country9,34
country10,31
country11,31
country12,30
country13,30
country14,29
country15,29
country16,25
country17,24
country18,23
country19,22
country20,22
country21,22
country22,21
country23,21
country24,21
country25,19
country26,18
country27,18
country28,15
country29,13
country30,41
country31,38
country32,16

The gadfly solution is presented at (Gadfly question: How to make Geom.bar slimmer, give alternating colors to bars and bars display their values)

This is the graphic to generate

How can I make similar bar chart in Makie?

Any help much appreciated.

Hello,

Here is my way of tackling with the problem:

I load the data to a dataframe as df.

Following section in the original post is not true, as pointed out by @nilshg

I had to convert dataframe columns to array in order to be able to use it.

country = convert(Array{String,1}, df.country)
percent = convert(Array{Int64,1}, df.percent)

The makie code:

barplot(1:length(percent), percent, 
	    direction=:x,
	    color=isodd.(1:length(percent)),
	    colormap = [:green, :blue],
	    bar_labels=:y, 
	    axis = (yticks = (1:length(percent), country),
                title = "Countries vs. Percentages"),)

Even though, the documentation is fairly good and the attributes are listed in the documentation and there are quite a few examples, the attributes lack the descriptions and I had to play with them without understanding what they are doing. I kinda did not like syntax even though it is flexible and classical. I may be wrong but Makie does not seem DataFrame friendly. Makie bargraph forced me to used only numeric values in x,y dimensions.

I really liked the structure of Gadfly syntax. However its documentation sucks, it is hard to find details of the attributes and how to use them properly.

It seems, I will be using both for a while by filling gaps of Gadfly where it is lacking with makie such as 3d plotting or realtime processing.

Makie has the approach, to offer a pretty non magical base for others to build upon more magical abstractions.
AlgebraOfGraphics is one of those, if you haven’t seen that yet :wink:
That should be closer to Gadfly, at least in regards of tables support.

1 Like

Just to add to @sdanisch, the reason categorical plots are currently a bit more cumbersome in Makie, is that for a mutable plotting approach, where you can iteratively add things to axes, and don’t just build a complete plot once, it’s pretty hard to get categorical things to work right. We used to have a simple recipe-like conversion, assign numbers from 1 to n to categories, plot, and set labels. But without complex additional infrastructure that we currently don’t have, you run into problems really quickly. What happens if you add a plot with different categories, how are these joined to the existing one, etc. What happens with other numerical values. So our approach right now is to stick to numerical-only, and labels can of course be set to whatever you please.

As AlgebraOfGraphics is a build-once kind of plotting package, it can offer a more convenient mode for working with categorical data, or dataframes in particular.

1 Like

Just to add that there’s no need to β€œconvert” DataFrames columns to vectors, they already are:

julia> df = DataFrame(country = ["country$i" for i ∈ 1:32], percent = rand(16:47, 32)); first(df, 5)
5Γ—2 DataFrame
 Row β”‚ country   percent 
     β”‚ String    Int64   
─────┼───────────────────
   1 β”‚ country1       26
   2 β”‚ country2       23
   3 β”‚ country3       35
   4 β”‚ country4       27
   5 β”‚ country5       37

julia> typeof(df.country)
Vector{String} (alias for Array{String, 1})

julia> typeof(df.percent)
Vector{Int64} (alias for Array{Int64, 1})
1 Like

Yes, I looked it up, but I have not been able to play with it much. It is in my bucket list to look into in more detail. Thanks for pointing out.

Yes, you are right. I was using Pluto. I do not know what I did, I received errors, but after converting the column, it worked. I checked again in REPL and yes, as you pointed out, DataFrames’ columns works without conversion. Thank you for correcting my mistake.

1 Like