# Create Groupedbar in Statsplot from Dataframe with dates on the x-axis

**URL:** <https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108>\
**Category:** Visualization\
**Tags:** statsplots\
**Created:** [July 12, 2022, 2:45pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108 "2022-07-12T14:45:31Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 12, 2022, 2:45pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/1 "2022-07-12T14:45:31Z")

</div>

Hi all,

I want to plot this csv:

 ![Capture d’écran_2022-07-12_21-29-55](https://global.discourse-cdn.com/julialang/original/3X/3/7/37b531f0431b14bbb0de06804af84609cc636e2b.png)

To become a groupedbar like this:

 ![Capture d’écran_2022-07-12_21-38-27](https://global.discourse-cdn.com/julialang/original/3X/1/9/1994ed6af21933e5b7992359e09d55aea9a94bf9.png)

1. With dates on the x-axis (do I need to change the format in the csv of 01.2015 / 02.2015?)
2. I want to plot an entire column (e.g. Solar) and plot to see the distribution with Distributions.jl package, since I saw this code I was inspired:

```julia
using Distributions
plot(Normal(3,5), fill=(0, .5,:orange))

```

the question is how? from my csv to become a distribution plot? Do I need to create a new csv just consisting of 2 columns: Month and Solar, is that right?

You can obtain the csv here:  
[https://energy-charts.info/charts/energy/chart.htm?l=en&c=DE&chartColumnSorting=default&interval=month&year=-1](https://energy-charts.info/charts/energy/chart.htm?l=en&c=DE&chartColumnSorting=default&interval=month&year=-1)

---

<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 13, 2022, 7:26am UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/2 "2022-07-13T07:26:33Z")

</div>

You can try the following:

```julia
using CSV, DataFrames, StatsPlots

df = CSV.read(filename, DataFrame)
df.Month = join.(split.(rpad.(string.(df.Month),6,"0"),'.'), '-')

groupedbar(df.Month, Matrix(df[:,2:end]), bar_position=:stack, bar_width=0.7, label=permutedims(names(df)[2:end]))

plot!(tickfontsize=6, xlabel="Month", ylabel="Energy(GWh)", guidefontsize=6, legendfontsize=6, legend=:outerright)

```

_ **NB:** there should be an easier way using the @df macro_

 ![StatsPlots_groupedbar_stack_electricity_Germany](https://global.discourse-cdn.com/julialang/original/3X/b/3/b37c10b6ac23631c504c10239930f93c2a09ba6c.png)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 13, 2022, 8:36am UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/3 "2022-07-13T08:36:37Z")

</div>

Can we plot thé whole months, January Till December in year 2019?

---

<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 13, 2022, 9:02am UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/4 "2022-07-13T09:02:36Z")

</div>

For such input file, the months are numbered from 1 to 12.  
If you are ok to plot them like that, just remove the line of code `df.Month = join...`, and it should work.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 13, 2022, 10:57am UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/5 "2022-07-13T10:57:15Z")

</div>

I get an error at this line:  
`groupedbar(df.Month, Matrix(df[:,2:end]), bar_position=:stack, bar_width=0.7, label=permutedims(names(df)[2:end])) `

 ![Capture d’écran_2022-07-13_17-55-57](https://global.discourse-cdn.com/julialang/original/3X/1/a/1a87ea38eb042b0632a2e7ddedc07c8d45f80503.png)

---

<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 13, 2022, 11:40am UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/6 "2022-07-13T11:40:57Z")

</div>

It seems that the file you loaded has `missing` data. It was not the case for any of the two files you suggested to look at above. What is the specific CSV file you are now using?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 13, 2022, 11:58am UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/7 "2022-07-13T11:58:52Z")

</div>

I use the option of all months  
 ![Capture d’écran_2022-07-13_18-58-29](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a1b4ec74eace9bab522d22219f3f606bdd5a492.png)

---

<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 13, 2022, 12:15pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/8 "2022-07-13T12:15:15Z")

</div>

Right. After you load the CSV into a dataframe, you can do: `describe(df)` to find out that the column 15 (Waste non-renewable) has missings: `Union{Missing, Float64}`

You may proceed as follows:

```julia
df[:,15] .= coalesce.(df[:,15], 0)
df.Month = join.(split.(rpad.(string.(df.Month),6,"0"),'.'),'-')

groupedbar(df.Month, Matrix(df[:,2:end]), bar_position=:stack, bar_width=0.7, label=permutedims(names(df)[2:end]))

plot!(tickfontsize=6, xlabel="Month", ylabel="Energy(GWh)", guidefontsize=6, legendfontsize=6, legend=:outerright)

```

It works but… you’ll run into a trickier problem: how to resize plots and fonts, when they are overloaded with information.

I suggest you look into Discourse and StackOverflow first, and if you’re not satisfied, open a new thread for advice.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 13, 2022, 12:37pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/9 "2022-07-13T12:37:40Z")

</div>

Thanks for the `describe(df)`

I will search discourse and Stackoverflow first before start a new thread.

---

<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 13, 2022, 4:48pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/10 "2022-07-13T16:48:57Z")

</div>

Fyi, the following quick settings display the last example fine on my system (_just showing edited lines_):

```julia
using Measures
plot(legendfontsize=6, widen=false, legend=:outerright, size=(1200,600), dpi=600, margins=10mm)
groupedbar!(df.Month, Matrix(df[:,2:end]), bar_position=:stack, lw=0.2, label=permutedims(names(df)[2:end]))
plot!(xrotation=90, tickfontsize=6, xlabel="Month", ylabel="Energy (GWh)", guidefontsize=7)

```

 ![StatsPlots_groupedbar_stack_electricity_Germany_montly__2015_2022](https://global.discourse-cdn.com/julialang/original/3X/a/7/a75c06d083eaf626571851fbdda55a8afc2d6054.jpeg)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 14, 2022, 1:47pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/11 "2022-07-14T13:47:12Z")

</div>

Wow amazing, I create multiple plots instead:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/1/91cb4a5448d186b9aa613e5f3279104506ca9dd1.png)

Thank you very much @rafael.guerra

---

<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 14, 2022, 1:48pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/12 "2022-07-14T13:48:44Z")

</div>

Thank you, because the sample data that you have shown is very educational about energy consumption in Germany, which is a key issue in Europe today.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 14, 2022, 2:09pm UTC](https://discourse.julialang.org/t/create-groupedbar-in-statsplot-from-dataframe-with-dates-on-the-x-axis/84108/13 "2022-07-14T14:09:04Z")

</div>

I have found it by accident as well from a blog talking about Julia and that person talk about energy and give the link to the database. I see it is a key issue now. Hopefully more renewable energy will be used in Europe.
