Bar plot proportions with same bar height = 1

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)

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:

Thank you for helping!

1 Like

I was doing something like:

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)

A question, there is an implementation of the tidiverse in Julia, did you try the TidierPlots?, maybe you can solve the problem by just copying and pasting that code

2 Likes

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

StatsPlots code
# 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))

1 Like

Bat signal @rdboyes

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

That’s perfect, thank you!

1 Like