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

**URL:** <https://discourse.julialang.org/t/gadfly-question-how-to-make-geom-bar-slimmer-give-alternating-colors-to-bars-and-bars-display-their-values/72327>\
**Category:** Visualization\
**Tags:** plotting, gadfly\
**Created:** [December 1, 2021, 12:26am UTC](https://discourse.julialang.org/t/gadfly-question-how-to-make-geom-bar-slimmer-give-alternating-colors-to-bars-and-bars-display-their-values/72327 "2021-12-01T00:26:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)\
**Post date:** [December 1, 2021, 12:26am UTC](https://discourse.julialang.org/t/gadfly-question-how-to-make-geom-bar-slimmer-give-alternating-colors-to-bars-and-bars-display-their-values/72327/1 "2021-12-01T00:26:34Z")

</div>

Hello,

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

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [December 1, 2021, 3:03am UTC](https://discourse.julialang.org/t/gadfly-question-how-to-make-geom-bar-slimmer-give-alternating-colors-to-bars-and-bars-display-their-values/72327/2 "2021-12-01T03:03:13Z")

</div>

Here’s my quick attempt at this:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/2/7/2791f77480991f6f1a8fe5b10b3009aab8d88dc5.png)

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](https://github.com/GiovineItalia/Gadfly.jl/issues/1542)), which I should find time to fix now.

---

<div class="post-metadata">

**Author:** ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)\
**Post date:** [December 1, 2021, 4:15am UTC](https://discourse.julialang.org/t/gadfly-question-how-to-make-geom-bar-slimmer-give-alternating-colors-to-bars-and-bars-display-their-values/72327/3 "2021-12-01T04:15:22Z")

</div>

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](https://discourse.julialang.org/t/real-time-plotting-with-gadfly-vehicle-trajectory-plotting/72333). I apprecite if you care to comment on that.

Again thank you very much.
