Skip to content

Instantly share code, notes, and snippets.

@mwdchang
Created June 18, 2024 19:47
Show Gist options
  • Save mwdchang/702a216c5dfe297c10f6e169979af0ff to your computer and use it in GitHub Desktop.
Save mwdchang/702a216c5dfe297c10f6e169979af0ff to your computer and use it in GitHub Desktop.
vega multiple time series + transform
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "Multi-series Line Chart",
"data": {
"values": [
{ "variable": "beta", "sample": 0, "time": 1, "value": 1 },
{ "variable": "beta", "sample": 0, "time": 2, "value": 2 },
{ "variable": "beta", "sample": 0, "time": 3, "value": 3 },
{ "variable": "beta", "sample": 0, "time": 4, "value": 3 },
{ "variable": "gamma", "sample": 0, "time": 1, "value": 1 },
{ "variable": "gamma", "sample": 0, "time": 2, "value": 0 },
{ "variable": "gamma", "sample": 0, "time": 3, "value": 1 },
{ "variable": "gamma", "sample": 0, "time": 4, "value": 1 },
{ "variable": "gamma", "sample": 1, "time": 1, "value": 0 },
{ "variable": "gamma", "sample": 1, "time": 2, "value": 2 },
{ "variable": "gamma", "sample": 1, "time": 3, "value": 4 },
{ "variable": "gamma", "sample": 1, "time": 4, "value": 2 },
]
},
"transform": [
{
"calculate": "datum.variable + datum.sample",
"as": "key"
},
{
"pivot": "key",
"value": "value",
"groupby": ["time"]
}
],
"repeat": {
"layer": ["beta0", "gamma0", "gamma1"]
},
"spec": {
"layer": [
{
"mark": {"type": "line", "stroke": "white", "strokeWidth": 4},
"encoding": {
"x": {"field": "time", "type": "quantitative"},
"y": {"field": {"repeat": "layer"}, "type": "quantitative", "title": "y-axis"},
// "stroke": {"datum": {"repeat": "layer"}, "type": "nominal"},
"stroke": {"datum": {"repeat": "layer"}, "type": "nominal"}
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment