# Stacked bars for different length

**URL:** https://discourse.julialang.org/t/stacked-bars-for-different-length/80577
**Category:** General Usage
**Tags:** visualization, statsplots
**Created:** [May 5, 2022, 11:20pm UTC](https://discourse.julialang.org/t/stacked-bars-for-different-length/80577 "2022-05-05T23:20:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Aminath\_Shausan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aminath_shausan/32/33587_2.png) [@Aminath\_Shausan](https://discourse.julialang.org/u/Aminath_Shausan)
#### Post date: [May 5, 2022, 11:20pm UTC](https://discourse.julialang.org/t/stacked-bars-for-different-length/80577/1 "2022-05-05T23:20:54Z")

</div>

Hi,  
I am trying to plot stacked bar graphs from the following data frame:

```julia
   name duration	label
   String3	Int64	SubStri…
1	AUS 31 1
2	AUS 89 2
3	AUS 133 3
4	GBR 86 1
5	GBR 88 2
6	GBR 117 3
7	GBR 55 4
8	IND 249 1
9	IND 120 2
10	USA 63 1
11	USA 221 2
12	USA 112 3
13	ITA 84 1 
14	ITA 133 2
15	ITA 111. 3
16	ITA 91 4

```

I would like the x-axis labels to be the ‘name’ column and y-axis to be ‘duration’ stacked with ‘label’. I thought of using StatsPlot ([stacked bar plot | [“Julia Plots Gallery”]](https://goropikari.github.io/PlotsGallery.jl/src/stacked_bar_plot.html)) package as per the example given. However, in my data frame, the number of ‘labels’ are not the same for each ‘name’. So was wondering if there is a way I could plot stacked bars for the data frame given. Any help is appreciated.  
Thanks

---

<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: [May 6, 2022, 7:31am UTC](https://discourse.julialang.org/t/stacked-bars-for-different-length/80577/2 "2022-05-06T07:31:35Z")

</div>

You could do:

```julia
using DataFrames, StatsPlots

countries = ("AUS","GBR","IND", "USA","ITA")
df = DataFrame(name=rand(countries,16), duration=rand(1:250,16), label=rand(1:4,16))

@df df groupedbar(:name, :duration, group=:label, ylabel="duration", bar_width=0.7, legend=:outertopright)

```

to get:

 ![StatsPlots_grouped_bar_countries](https://global.discourse-cdn.com/julialang/original/3X/e/7/e7ace73e9dedf8be8d6977e25893a266fca7afb7.png)

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [May 6, 2022, 12:07pm UTC](https://discourse.julialang.org/t/stacked-bars-for-different-length/80577/3 "2022-05-06T12:07:08Z")

</div>

```julia
@df df groupedbar(:name, :duration, group=:label, ylabel="duration",
 bar_width=0.7, legend=:outertopright, bar_position=:stack)

```

![grafik](https://global.discourse-cdn.com/julialang/original/3X/a/4/a460db8ed0c2a6025a256f70e83f40f2c433d00d.png)

```julia
@df df groupedbar(:label, :duration, group=:name, ylabel="duration",
 bar_width=0.7, legend=:outertopright, bar_position=:stack)

```

![grafik](https://global.discourse-cdn.com/julialang/original/3X/e/5/e5575c99c068f655a7e6e8b8607ba085ab6fed08.png)
