# Plotting multiple datasets in the same chart in VegaLite

**URL:** <https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964>\
**Category:** Visualization\
**Created:** [September 14, 2018, 4:25pm UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964 "2018-09-14T16:25:04Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [September 14, 2018, 4:25pm UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964/1 "2018-09-14T16:25:04Z")

</div>

Is it possible to plot multiple datasets in the same VegaLite chart without merging them beforehand? I thought “adding” two `@vlplot` statements like this would work, but it only displays the sp500 data.

```julia
using VegaLite, Query, VegaDatasets

goog = dataset("stocks") |> @filter(_.symbol == "GOOG")
sp500 = dataset("sp500")

@vlplot(mark={:line, color=:red}, data=goog, x="date:t", y=:price) +
    @vlplot(mark={:line, color=:black}, data=sp500, x="date:t", y=:price)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 14, 2018, 5:25pm UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964/2 "2018-09-14T17:25:10Z")

</div>

`@vlplot() + @vlplot(mark={:line, color=:red}, data=goog, x="date:t", y=:price) + @vlplot(mark={:line, color=:black}, data=sp500, x="date:t", y=:price)`

see examples:

[http://fredo-dedup.github.io/VegaLite.jl/stable/examples/examples\_error\_bars\_bands.html](http://fredo-dedup.github.io/VegaLite.jl/stable/examples/examples_error_bars_bands.html)

---

<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:** [September 14, 2018, 5:32pm UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964/3 "2018-09-14T17:32:50Z")

</div>

Thanks! Now that you bring it up, I did notice that bizarre empty `@vlplot()` statement earlier when I browsed the examples, but I didn’t connect it with this. Can you explain what it does?

---

<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:** [September 15, 2018, 12:00am UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964/4 "2018-09-15T00:00:04Z")

</div>

I really need to write the doc for composite plots 🙂 But, I’m still not a 100% sure I got the API right, so maybe some feedback first would be great.

The logic here is the following: the first `@vlplot()` creates an empty spec. The `+` operator then does the following: it adds the thing on the right hand side inside a `layer` elment to the thing on the left hand side. In this case here, the second and the third `@vlplot` things become entries in the `layer` element of the spec created by the first empty `@vlplot()` call.

Now that I’m thinking about this, I can’t fully understand why I didn’t just make this work without this first `@vlplot()` call though…

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 15, 2018, 10:21am UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964/5 "2018-09-15T10:21:31Z")

</div>

I don’t see any issues here.

The first empty @vlplot() doesn’t seem “bizarre” to me. Maybe it is just not intuitive in the special example where @vlplot() seems to be an empty call.

If you look at it as the initial call to set all defaults for the overall setting of the image it is very intuitive. Example:

```julia
using VegaLite, Query, VegaDatasets

goog = dataset("stocks") |> @filter(_.symbol == "GOOG")
sp500 = dataset("sp500")
msft = dataset("stocks") |> @filter(_.symbol == "MSFT")

@vlplot(width=400,height=400) +
@vlplot(mark={:line, color=:red}, data=goog, x="date:t", y=:price) +
@vlplot(mark={:line, color=:black}, data=sp500, x="date:t", y=:price) +
@vlplot(mark={:line, color=:green}, data=msft, x="date:t", y=:price)

```

![visualization](https://global.discourse-cdn.com/julialang/original/3X/6/3/6328968c99e7af1a83feca5f274a2abc9f5d5e8c.png)

---

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [September 18, 2018, 9:41am UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964/6 "2018-09-18T09:41:27Z")

</div>

How do one get a legend in the above example by [oheil](https://discourse.julialang.org/u/oheil)?

And how do I get a grouped bar chart with the same data as above? All the examples I have seen about grouped bar charts and normalized bar charts work with differentiating data in the same field.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 19, 2018, 1:41pm UTC](https://discourse.julialang.org/t/plotting-multiple-datasets-in-the-same-chart-in-vegalite/14964/7 "2018-09-19T13:41:33Z")

</div>

Adding a legend to the plot is not straight forward for me.

This is my solution for reproducing the plot with legend:

```julia
using VegaLite, Query, VegaDatasets
using DataFrames

goog = dataset("stocks") |> @filter(_.symbol == "GOOG")
sp500 = DataFrame(dataset("sp500"))
sp500[:symbol]="sp500"
msft = dataset("stocks") |> @filter(_.symbol == "MSFT")

@vlplot(width=400,height=400, color={field="symbol",scale={range=["black","red","green"]}}) +
@vlplot(mark={:line,color=:red}, data=goog, x="date:t", y=:price) +
@vlplot(mark={:line,color=:black}, data=sp500, x="date:t", y=:price) +
@vlplot(mark={:line,color=:green}, data=msft, x="date:t", y=:price)

```

The colors in the mark specification are ignored and superseded by the color range in the scale definition.

As far as I understand the [vega-lite documentation](https://vega.github.io/vega-lite/docs/) it seems to be not possible to create [grouped bar charts](http://fredo-dedup.github.io/VegaLite.jl/stable/examples/examples_barchartshistograms.html#Grouped-Bar-Chart-1) using [Layering Views](https://vega.github.io/vega-lite/docs/layer.html) (created in VegaLite.jl with the + -Operator and documented under [Layered Plots](http://fredo-dedup.github.io/VegaLite.jl/stable/examples/examples_error_bars_bands.html)).  
For grouped bar charts you have to use [vega-lite Faceting](https://vega.github.io/vega-lite/docs/facet.html) which is, as you say, done by “differentiating data in the same field”, see [vega-lite grouped bar chart](https://vega.github.io/vega-lite/docs/bar.html#grouped-bar-chart).

With the data above this could be achieved with:

```julia
alldata=vcat(sp500,DataFrame(dataset("stocks")))

@vlplot(:bar, transform=[{filter={field="symbol","oneOf"=["sp500","GOOG","MSFT"]}}],data=alldata, enc={column="date:n",x={field="symbol",type="nominal",axis={title=""}}, y=:price}, color=:symbol)

```

.
