# How to add count legend on Bar graph using Gadfly?

**URL:** <https://discourse.julialang.org/t/how-to-add-count-legend-on-bar-graph-using-gadfly/67197>\
**Category:** Visualization\
**Tags:** plotting, gadfly\
**Created:** [August 27, 2021, 11:31pm UTC](https://discourse.julialang.org/t/how-to-add-count-legend-on-bar-graph-using-gadfly/67197 "2021-08-27T23:31:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wakimchris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wakimchris/32/21325_2.png) [@wakimchris](https://discourse.julialang.org/u/wakimchris)\
**Post date:** [August 27, 2021, 11:31pm UTC](https://discourse.julialang.org/t/how-to-add-count-legend-on-bar-graph-using-gadfly/67197/1 "2021-08-27T23:31:48Z")

</div>

I have created a DataFrame such as

```julia
│ Row │ Gender_Code │ N │
│ │ String │ Int64 │
├─────┼─────────────┼───────┤
│ 1 │ M │ 1789 │
│ 2 │ F │ 205 │

```

When I applied on it Geom.bar, the bars don’t have the frequency of the M vs F.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/4/34a88a67e777a929427c2c7ebfd06eaf3376534a.png)

How to add count legend on Bar graph.

---

<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:** [August 28, 2021, 12:37am UTC](https://discourse.julialang.org/t/how-to-add-count-legend-on-bar-graph-using-gadfly/67197/2 "2021-08-28T00:37:35Z")

</div>

See the [Stat.dodge example](http://gadflyjl.org/stable/gallery/statistics/#%5BStat.dodge%5D(@ref)) in the Gadfly docs.

---

<div class="post-metadata">

**Author:** ![wakimchris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wakimchris/32/21325_2.png) [@wakimchris](https://discourse.julialang.org/u/wakimchris)\
**Post date:** [August 28, 2021, 6:22am UTC](https://discourse.julialang.org/t/how-to-add-count-legend-on-bar-graph-using-gadfly/67197/3 "2021-08-28T06:22:09Z")

</div>

If I have

```julia
│ Row │ Age │ N │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 46 │ 3 │
│ 2 │ 45 │ 58 │
│ 3 │ 44 │ 86 │
│ 4 │ 43 │ 131 │

```

then my command of

```julia
num_age_sorted_byFrequency.label = string.(round.(Int, num_age_sorted_byFrequency.N))
Age_Frequency_barplot = plot(num_age_sorted_byFrequency, x=:Age, y=:N, label=:label, Geom.label(position=:centered), Stat.dodge(position=:stack), Geom.bar(position=:stack))

```

works

But when I have a

```julia
│ Row │ Gender_Code │ N │
│ │ String │ Int64 │
├─────┼─────────────┼───────┤
│ 1 │ M │ 1789 │
│ 2 │ F │ 205 │

```

and I apply

```julia
num_Gender_Code_sorted_byFrequency.label = string.(round.(Int, num_Gender_Code_sorted_byFrequency.N))
Gender_Code_Frequency_barplot = plot(num_Gender_Code_sorted_byFrequency, x=:Gender_Code, y=:N, label=:label, Geom.label(position=:centered), Stat.dodge(position=:stack), Geom.bar(position=:stack))

```

I get

```julia
Error showing value of type Plot:
ERROR: MethodError: no method matching -(::String, ::String)

```

---

<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:** [August 28, 2021, 8:42am UTC](https://discourse.julialang.org/t/how-to-add-count-legend-on-bar-graph-using-gadfly/67197/4 "2021-08-28T08:42:02Z")

</div>

Ok, I see now. In this example with `Geom.bar`, `Stat.dodge` isn’t needed:

```julia
plot(df, x=:Gender_Code, y=:N, Geom.bar, label=string.(df.N), 
    Geom.label(position=:above))

```

With `Stat.dodge`, for some reason it’s necessary to add `Scale.x_discrete`:

```julia
plot(df, x=:Gender_Code, y=:N, label=string.(df.N),
    Geom.label(position=:above), Stat.dodge, Geom.bar,
    Scale.x_discrete)

```

and I should investigate why!
