Adding a legend to the plot is not straight forward for me.
This is my solution for reproducing the plot with legend:
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 it seems to be not possible to create grouped bar charts using Layering Views (created in VegaLite.jl with the + -Operator and documented under Layered Plots).
For grouped bar charts you have to use vega-lite Faceting which is, as you say, done by “differentiating data in the same field”, see vega-lite grouped bar chart.
With the data above this could be achieved with:
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)
.