# Bar plot proportions with same bar height = 1

**URL:** <https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566>\
**Category:** Visualization\
**Tags:** plotting, statsplots\
**Created:** [July 28, 2024, 12:53pm UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566 "2024-07-28T12:53:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elisadonati23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elisadonati23/32/205472_2.png) [@elisadonati23](https://discourse.julialang.org/u/elisadonati23)\
**Post date:** [July 28, 2024, 12:53pm UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566/1 "2024-07-28T12:53:37Z")

</div>

Hi all! Quite new into julia and struggling a lot with plots. I am moving from R and I cannot find a way to do a similar plot (using ggplot2)

```julia
ggplot(propdf, aes(x = date, y = prop, fill = age_year)) +
  geom_bar(stat = "identity", position = "stack") +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  scale_fill_viridis_d(option = "F", direction = -1) + # Use the viridis palette
  labs(title = "Proportion of Each Age Mid Year Over Time - Low food",
       x = "Time",
       y = "Proportion",
       fill = "Age Year") +
  theme_minimal()

```

which results in the file attach:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/dea422cc3296f20acdafe0f69b193ec4726fe225.png)

Thank you for helping!

---

<div class="post-metadata">

**Author:** ![elisadonati23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elisadonati23/32/205472_2.png) [@elisadonati23](https://discourse.julialang.org/u/elisadonati23)\
**Post date:** [July 28, 2024, 12:59pm UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566/2 "2024-07-28T12:59:10Z")

</div>

I was doing something like:

```julia
I# Define a color palette
colors = palette(:viridis, length(unique(propdf.age_year)))

# Plotting
bar(propdf.date, propdf.prop, group=propdf.age_year, xlabel="Time", ylabel="Proportion", 
    title="Proportion of Each Age Mid Year Over Time - Low food", legend=:topright, 
    seriestype=:stack, fillcolor=colors)

```

with no much success (colors were the same for the categories and bars had different heights)

---

<div class="post-metadata">

**Author:** ![indymnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/indymnv/32/212594_2.png) [@indymnv](https://discourse.julialang.org/u/indymnv)\
**Post date:** [July 28, 2024, 3:10pm UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566/3 "2024-07-28T15:10:02Z")

</div>

A question, there is an implementation of the tidiverse in Julia, did you try the [TidierPlots](https://github.com/TidierOrg/TidierPlots.jl)?, maybe you can solve the problem by just copying and pasting that code

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 28, 2024, 10:57pm UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566/4 "2024-07-28T22:57:11Z")

</div>

Take a look at the following StatsPlots.jl code in case it helps:

> **StatsPlots code**
>
> ```julia
> # 1 - Create sample data (*easier way please*)
> 
> using Random, DataFrames
> df = DataFrame(year=rand(1930:2020, 10000), age_year=rand(0:17, 10000))
> df2 = combine(groupby(df, [:year, :age_year]), :age_year => length => :num)
> df3 = combine(groupby(df, :year), :age_year => length => :den)
> df4 = outerjoin(df3, df2, on=:year)
> df4.prop = df4.num./df4.den
> dg = outerjoin(df, df4, on=[:year, :age_year])
> 
> # 2 - Plot data using StatsPlots
> 
> using StatsPlots, Measures
> colors = permutedims(collect(palette(:viridis, length(unique(dg.age_year)))))
> groupedbar(dg.year, 100*dg.prop, group=dg.age_year, xlabel="Time", ylabel="Proportion [%]",
> legend=:outerright, bar_position=:stack, colorbar_title="Age Year", c=colors, lw=0,
> tick_direction=:out, dpi=600, size=(1000,600), margins=5mm
> )
> annotate!(maximum(dg.year) + 17, 75, text("Age Year", 9))
> 
> ```

 ![StatsPlots_groupedbar_proportions](https://global.discourse-cdn.com/julialang/original/3X/c/a/cae81f2373299396209395cc0c5bda653f72c64d.png)

---

<div class="post-metadata">

**Author:** ![kdpsingh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdpsingh/32/215018_2.png) [@kdpsingh](https://discourse.julialang.org/u/kdpsingh)\
**Post date:** [July 29, 2024, 4:40am UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566/5 "2024-07-29T04:40:08Z")

</div>

Bat signal @rdboyes

---

<div class="post-metadata">

**Author:** ![elisadonati23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elisadonati23/32/205472_2.png) [@elisadonati23](https://discourse.julialang.org/u/elisadonati23)\
**Post date:** [July 29, 2024, 10:52am UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566/6 "2024-07-29T10:52:25Z")

</div>

I didn’t know @indymnv ! Thank you, I will try

---

<div class="post-metadata">

**Author:** ![elisadonati23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elisadonati23/32/205472_2.png) [@elisadonati23](https://discourse.julialang.org/u/elisadonati23)\
**Post date:** [July 29, 2024, 10:52am UTC](https://discourse.julialang.org/t/bar-plot-proportions-with-same-bar-height-1/117566/7 "2024-07-29T10:52:45Z")

</div>

That’s perfect, thank you!
