Gadfly question: How to make Geom.bar slimmer, give alternating colors to bars and bars display their values

Hello,

I am trying to learn gadfly plotting library. I have a very simple dataset as follows:

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

I am trying to create bar graph, and bars should be on the y axis and they should be horizontally displayed based on the values on x axis. X axis will show the measure of percentages

My gadfly plot command is as follows:

plot(df, x=:percent, y=:country, Geom.bar(orientation=:horizontal), Guide.ylabel("country", orientation=:vertical))

I need to polish the final graph but I could not figure out how to do it. Gadfly documentation, unfortunately is very lacking when it comes to details of the api.

The things that I want to improve or do:

  1. Currently, bars are very thick and there are no spaces between them. I want spaces if possible. They look like overlapping even if I use position=:dodge setting for the bar geometry. How can I insert spaces between bars and/or make bars thinner.

  2. I have given color=:country aesthetics but the resulting graph was to colorful. I want only for example two colors and bars should have those colors in alternating way to improve readability. I know that I can do it via adding a third column to the data which may have one of two values for each row and based on that I can use a color = :Column3. I hope or believe there must be better and easier way to do it.

  3. I would like display the values of bars at the tip or inside of them.

Any insight, help much appreciated.

Here’s my quick attempt at this:

plot(df, y=:country, x=:percent, Geom.bar(orientation=:horizontal),
    Geom.label(position=:right), label=string.(df.percent),
    Guide.ylabel(""), color=isodd.(axes(df,1)), 
    Theme(bar_spacing=1mm, key_position=:none))

benibilme

But note there is a small bug in the code when Geom.bar is used with color (Bar spacing shifting stacked plot off center · Issue #1542 · GiovineItalia/Gadfly.jl · GitHub), which I should find time to fix now.

2 Likes

Thank you. This is exactly what I want. I really appreciate.

Unfortunately, it looks intimidating to me. I do not think, I would be able to come up with that. After seeing bar_spacing setting in your solution, I finally realized its place and by searching, I found where it is embedded. color=isodd.(axes(df,1)) is a brilliant solution which I would have difficulty to come by with my inadequate julia experience.

I have another question about plotting trajectory from datastream via gadfly at my question. I apprecite if you care to comment on that.

Again thank you very much.