# How to plot a simple stacked area chart

**URL:** https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351
**Category:** General Usage
**Tags:** plotting
**Created:** [March 1, 2019, 3:35pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351 "2019-03-01T15:35:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 1, 2019, 3:35pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351/1 "2019-03-01T15:35:23Z")

</div>

All I need to do is to plot a simple stacked chart (possibly also normalised).

I would like to start from these simple data:

```julia
a = [1,1,1,1.5,2,3]
b = [0.5,0.6,0.4,0.3,0.3,0.2]
c = [2,1.8,2.2,3.3,2.5,1.8]
sNames = ["a","b","c"]
xLabels = [2001,2002,2003,2004,2005,2006]

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/1/4106f737e23ac4b2b3c77b5403d550fd1c1c8b29.png)

I did try:

- PlotRecipes (now “GraphRecipes”) but [the example given](https://github.com/JuliaPlots/Plots.jl/issues/1423) is too complex and it doesn’t run on Julia 1.1
- Vega.jl. [The example](http://johnmyleswhite.github.io/Vega.jl/areaplot.html) seems simple enought, but the package doesn’t work on Julia \>= 0.7
- VegaLite.jl. [The example](http://fredo-dedup.github.io/VegaLite.jl/stable/examples/examples_area_Charts_streamgraphs/) is very complex too, and I didn’t manage to reduce it to my simpler case
- [groupedbar recipe of StatsPlots](https://github.com/JuliaPlots/StatsPlots.jl#grouped-bar-plots), but it’s a histogram not a stacked area chart, and it is not indicated how to get it normalised (aside of course manually doing that ex-ante)

I have to say I’m a bit frustrated… I may just be dumb, I feel like in the domain of plotting there is a competition on showing cool features you can get with the various plotting packages, instead of showing simple examples that highlight one specific point at the time…

---

<div class="post-metadata">

### Author: ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)
#### Post date: [March 1, 2019, 3:57pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351/2 "2019-03-01T15:57:46Z")

</div>

Here’s one way:

```julia
using Plots

@userplot StackedArea

# a simple "recipe" for Plots.jl to get stacked area plots
# usage: stackedarea(xvector, datamatrix, plotsoptions)
@recipe function f(pc::StackedArea)
    x, y = pc.args
    n = length(x)
    y = cumsum(y, dims=2)
    seriestype := :shape

    # create a filled polygon for each item
    for c=1:size(y,2)
        sx = vcat(x, reverse(x))
        sy = vcat(y[:,c], c==1 ? zeros(n) : reverse(y[:,c-1]))
        @series (sx, sy)
    end
end

a = [1,1,1,1.5,2,3]
b = [0.5,0.6,0.4,0.3,0.3,0.2]
c = [2,1.8,2.2,3.3,2.5,1.8]
sNames = ["a","b","c"]
x = [2001,2002,2003,2004,2005,2006]

plotly()
stackedarea(x, [a b c], labels=reshape(sNames, (1,3)))

```

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

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 1, 2019, 4:26pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351/3 "2019-03-01T16:26:08Z")

</div>

Thank you… I guess that if I want a “normalise” option I need to change the logic inside that function… but how to pass it a further, non-plots parameter ? like this?:

```julia
@recipe function f(pc::StackedArea; normalise=false)
[...]

```

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [March 1, 2019, 6:05pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351/4 "2019-03-01T18:05:15Z")

</div>

The trick for [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) (and presumably any grammar of graphics like package) is that you first need to get your data into a tidy format, and then can plot it.

If I start out putting this into a `DataFrame`:

```julia
using DataFrames

a = [1,1,1,1.5,2,3]
b = [0.5,0.6,0.4,0.3,0.3,0.2]
c = [2,1.8,2.2,3.3,2.5,1.8]
sNames = ["a","b","c"]
xLabels = [2001,2002,2003,2004,2005,2006]

df = DataFrame(year=xLabels, a=a, b=b, c=c)

```

then one can put it into tidy format easily with `stack`:

```julia
julia> df |> stack
18×3 DataFrame
│ Row │ variable │ value │ year │
│ │ Symbol │ Float64 │ Int64 │
├─────┼──────────┼─────────┼───────┤
│ 1 │ a │ 1.0 │ 2001 │
│ 2 │ a │ 1.0 │ 2002 │
│ 3 │ a │ 1.0 │ 2003 │
│ 4 │ a │ 1.5 │ 2004 │
│ 5 │ a │ 2.0 │ 2005 │
│ 6 │ a │ 3.0 │ 2006 │
│ 7 │ b │ 0.5 │ 2001 │
│ 8 │ b │ 0.6 │ 2002 │
│ 9 │ b │ 0.4 │ 2003 │
│ 10 │ b │ 0.3 │ 2004 │
│ 11 │ b │ 0.3 │ 2005 │
│ 12 │ b │ 0.2 │ 2006 │
│ 13 │ c │ 2.0 │ 2001 │
│ 14 │ c │ 1.8 │ 2002 │
│ 15 │ c │ 2.2 │ 2003 │
│ 16 │ c │ 3.3 │ 2004 │
│ 17 │ c │ 2.5 │ 2005 │
│ 18 │ c │ 1.8 │ 2006 │

```

From there on it is pretty simple with [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl). Here is the base version:

```julia
df |>
stack |>
@vlplot(:area, x=:year, y={:value, stack=:zero}, color="variable:n")

```

That gives me:

![visualization%20(5)](https://global.discourse-cdn.com/julialang/original/3X/2/7/272476b9f05d228e587a1c1f6161d81b8bf8837c.png)

And normalized:

```julia
df |>
stack |>
@vlplot(:area, x=:year, y={:value, stack=:normalize}, color="variable:n")

```

looks like this:

![visualization%20(6)](https://global.discourse-cdn.com/julialang/original/3X/2/d/2dacad352c37708313c55ab428d96490c88a4458.png)

---

<div class="post-metadata">

### Author: ![willow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willow/32/208918_2.png) [@willow](https://discourse.julialang.org/u/willow)
#### Post date: [February 4, 2020, 1:40am UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351/5 "2020-02-04T01:40:51Z")

</div>

Plots.jl now ships with the [`areaplot`](https://github.com/JuliaPlots/Plots.jl/blob/3459bcd81766f71ba6ff0d185bf9e32cc5425521/src/recipes.jl#L1195) command.

---

<div class="post-metadata">

### Author: ![claytonpbarrows](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/claytonpbarrows/32/14233_2.png) [@claytonpbarrows](https://discourse.julialang.org/u/claytonpbarrows)
#### Post date: [April 20, 2020, 8:29pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351/6 "2020-04-20T20:29:21Z")

</div>

Does anyone know how to make a recipe for a stacked area plot so that the PlotlyJS backend behavior resembles the figure created by the following code?

```julia
using PlotlyJS
function area()
    traces = [PlotlyJS.scatter(;x=1:3, y=[2, 1, 4], fill="tonexty", stackgroup = "one",),
    PlotlyJS.scatter(;x=1:3, y=[1, 1, 2], fill="tonexty", stackgroup = "one",),
    PlotlyJS.scatter(;x=1:3, y=[3, 0, 2], fill="tonexty", stackgroup = "one",)]

    PlotlyJS.plot(traces, PlotlyJS.Layout(title="stacked and filled line chart"))
end
area()

```
