Dodged barplot got thin out in Makie

Hello everyone. Sorry if my question is too basic for some but I don’t understand why my barplot got thin out after I try to add another set of dodged bars. Below is an example. Suppose that I have some data like this:

dat = DataFrame(x = [90, 75, 150], y = [120, 100, 350])

and here is what I get using the following piece of code:

fig = Figure(resolution = (1920, 1080))

update_theme!(with_theme = theme_light(), fontsize = 30)

axis = fig[1, 1] = Axis(fig, title = "",
                        xlabel = "Index",
                        xticks = (1:3),
                        ylabel = "Count")

barplot!(axis, 1:3, dat.x)

fig

To give some perspective, here is the plot without the dodge bar:

So please, does anyone knows the solution? Also, how can I set the color of both the main bars and the dodged bar instead of just the main? I’ve searched the documentation and yet found not much things related. Thank you in advance!

I don’t see any dodging in your code, you’re sure you copied it correctly?

Hi, thanks for replying! You’re right, the piece of code above is for the second plot. Here is for the first one. I used and followed the example in the documentation for dodged bar. Notice that the dodged bars are not being displayed, and the the main bars get thin out significantly.

fig = Figure(resolution = (1920, 1080))

update_theme!(with_theme = theme_light(), fontsize = 30)

axis = fig[1, 1] = Axis(fig, title = "",
                        xlabel = "Index",
                        xticks = (1:3),
                        ylabel = "Count")

barplot!(axis, 1:3, dat.x, dodge = dat.y)

fig

It is not very clear from your example what exactly you are trying to accomplish, so it isn’t clear how to fix the code in your example.

However, I think your problem is that you are using y (I assume that is your dependent variable) as your dodge value. You might want to read the documentation for dodge again. It is a categorical variable which indicates which dodge group each data point should be from. For example, if you have three categories of variable you want to show, your dodge value should be an integer from 1-3. (Not the y value, e.g. bar height).

1 Like

Here’s an example that uses dodging:

fig = Figure()
ax = Axis(fig[1,1])
barplot!(ax, repeat(1:3, 2), rand(6), dodge=repeat(1:2, outer=3), color=repeat(1:2, outer=3))

It will make a figure like this:

Here is the documentation: boxplot

Unfortunately Makie’s documentation still has some gaps in it. In general my experience with Makie is that there are some cases where the online documentation is better and other cases where the REPL documentation is better. I would check both.

Hi, thanks for your responses. My idea is that I want to plot the value of x next to the value of y pairwise using a dodged barplot. Your comment was correct, after some struggle time with the documentation and of course your example, I finally ccame up with the solution. Below you can find the code and the plot output:

dat = DataFrame(x = [90, 75, 150], y = [120, 100, 350])

# Transform plot data
using Pipe

plot_dat = @pipe dat |> Matrix |> transpose |> reshape(_, (6, 1)) |> vec

# Plot
set_theme!()

fig = Figure()

axis = fig[1,1] = Axis(fig, title = "",
                        ylabel = "Count")

dodge = repeat(1:2, outer = 3)

barplot!(axis, repeat(1:3, inner = 2), plot_dat, 
        dodge = dodge, 
        color = map(x -> x == 1 ? "#c94c4c" : "#034f84", dodge))

# Return the final output
fig

I’m not sure the reason why but for me, generating statistical plots using Makie is somewhat more difficult and cumbersome compared to ggpolt2 or Matplotlib (as you can see I have to transform my data frame to matrix form, transpose it, then reshape to into a column matrix and then convert to a vector, quite a lot of steps!) With that being said, I think that’s the challenge that one has to overcome.

You have a wide dataframe but need a tall one for this kind of call (I think that’s also the case with ggplot2?). Your reshape operation is your own version of stack which you could use for that. Granted, having to manually convert a categorical column to Int is annoying, but Makie is not AlgebraOfGraphics and we sometimes avoid getting too high-level in our APIs, as we’re more concerned with the low-level plot objects. In AlgebraOfGraphics, the dodged barplot is much easier to do because it handles the categorical conversion for you (actually the Makie API of barplot was exactly written for AoG which outputs vectors of integers).

1 Like