Plotting bar chart inside a loop


#1

I would like to plot a bar chart like this following one :
Sans%20titre

The x-axe data are represented by dates

x_years = [ "2018", "2019"]

and my y-axe data represents the amount of production of a given product. This information comes from a dictionary I have created.

amount_product[productA,NY] = [ 10, 15 ]

That means that were produced 10 products A in 2018 and 15 in 2019, in NY region.
I have tried the following code to try to build this:

plt= []
for product in list_product   
    for r in list_regions
            plt = bar!(x_years, amount_product[product,r])
        end
    end
end
display(plt)

I got the following result
newplot
I know it’s like plt only keep the last value from the dictionary but I don’t know how to make it differently. Anyone has an idea to help me build this kind of bar chart? Thanks in advance.


#2

Use groupedbar (once you get StatPlots installed) https://github.com/JuliaPlots/StatPlots.jl#grouped-bar-plots
.


#3

With VegaLite.jl you can do it this way:

using VegaLite, DataFrames

# Create a DataFrame in tidy format
df = DataFrame(
    product=["A", "A", "B", "B"],
    year=[2018, 2019, 2018, 2019],
    amount=[4., 5., 3, .6])

# Plot
df |> @vlplot(:bar, x="year:N", y=:amount, color=:product)

And the plot will look like this:
plot