Skip to content

Instantly share code, notes, and snippets.

@xverges
Last active April 7, 2021 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xverges/f7ed509b29342059b29cb03f24c03239 to your computer and use it in GitHub Desktop.
Save xverges/f7ed509b29342059b29cb03f24c03239 to your computer and use it in GitHub Desktop.
Vega Vertical Violin Plot
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A violin plot example showing distributions for pengiun body mass.",
"height": 400,
"padding": 5,
"config": {
"axisBand": {
"bandPosition": 1,
"tickExtra": true,
"tickOffset": 0
}
},
"signals": [
{
"name": "plotWidth",
"value": 100
},
{
"name": "width",
"update": "(plotWidth + 10) * 3"
},
{
"name": "trim",
"value": false,
"bind": {
"input": "checkbox"
}
},
{
"name": "bandwidth",
"value": 0,
"bind": {
"input": "range",
"min": 0,
"max": 200,
"step": 1
}
}
],
"data": [
{
"name": "density",
"values": [
{
"value": 2850,
"density": 0.0002003060091018729
},
{
"value": 2927,
"density": 0.00027738799539219427
},
{
"value": 3004,
"density": 0.0003575734196341432
},
{
"value": 3081,
"density": 0.00043809546236366905
},
{
"value": 3158,
"density": 0.0005181683517365764
},
{
"value": 3235,
"density": 0.000596382505269132
},
{
"value": 3312,
"density": 0.0006682439693157432
},
{
"value": 3389,
"density": 0.0007262881276328334
},
{
"value": 3466,
"density": 0.0007637649669409242
},
{
"value": 3543,
"density": 0.0007792299398584311
},
{
"value": 3620,
"density": 0.0007770121047160155
},
{
"value": 3697,
"density": 0.0007622423196332618
},
{
"value": 3774,
"density": 0.0007357996208426459
},
{
"value": 3851,
"density": 0.0006951533761433165
},
{
"value": 3928,
"density": 0.0006401661353984976
},
{
"value": 4005,
"density": 0.0005771015073096618
},
{
"value": 4082,
"density": 0.0005159470243049639
},
{
"value": 4159,
"density": 0.0004632835782433325
},
{
"value": 4236,
"density": 0.00041790582326110533
},
{
"value": 4313,
"density": 0.00037372055361244947
},
{
"value": 4390,
"density": 0.0003265587413568889
},
{
"value": 4467,
"density": 0.0002775822234113885
},
{
"value": 4544,
"density": 0.00023056666271721515
},
{
"value": 4621,
"density": 0.00018735168862749085
},
{
"value": 4698,
"density": 0.00014684527120538605
},
{
"value": 4775,
"density": 0.00010791624833637362
}
]
}
],
"scales": [
{
"name": "yscale",
"type": "linear",
"range": "height",
"round": true,
"domain": {
"data": "density",
"field": "value"
},
"domainMin": 2000,
"zero": false,
"nice": true
},
{
"name": "xscale",
"type": "linear",
"range": [
0,
{
"signal": "plotWidth"
}
],
"domain": {
"data": "density",
"field": "density"
}
}
],
"axes": [
{
"orient": "left",
"scale": "yscale",
"zindex": 1
},
{
"orient": "bottom",
"scale": "xscale",
"zindex": 1
}
],
"marks": [
{
"type": "area",
"orient": "horizontal",
"defined": true,
"opacity": 1,
"fillOpacity": 1,
"from": {
"data": "density"
},
"encode": {
"enter": {
"orient": {
"value": "horizontal"
},
"fill": {
"value": "steelblue"
}
},
"update": {
"y": {
"scale": "yscale",
"field": "value"
},
"x": {
"value": 0
},
"x2": {
"scale": "xscale",
"field": "density"
}
}
}
},
{
"type": "symbol",
"from": {
"data": "density"
},
"encode": {
"update": {
"x": {
"scale": "xscale",
"field": "density"
},
"y": {
"scale": "yscale",
"field": "value"
},
"shape": {
"value": "circle"
},
"strokeWidth": {
"value": 2
},
"opacity": {
"value": 1
},
"stroke": {
"value": "red"
},
"fill": {
"value": "transparent"
}
}
}
}
]
}
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A violin plot example showing distributions for pengiun body mass.",
"height": 400,
"padding": 5,
"config": {
"axisBand": {
"bandPosition": 1,
"tickExtra": true,
"tickOffset": 0
}
},
"signals": [
{
"name": "plotWidth",
"value": 100
},
{
"name": "width",
"update": "(plotWidth + 10) * 3"
},
{
"name": "trim",
"value": false,
"bind": {
"input": "checkbox"
}
},
{
"name": "bandwidth",
"value": 0,
"bind": {
"input": "range",
"min": 0,
"max": 200,
"step": 1
}
}
],
"data": [
{
"name": "penguins",
"url": "data/penguins.json",
"transform": [
{
"type": "filter",
"expr": "datum.Species != null && datum['Body Mass (g)'] != null"
}
]
},
{
"name": "density",
"source": "penguins",
"transform": [
{
"type": "kde",
"field": "Body Mass (g)",
"groupby": [
"Species"
],
"bandwidth": {
"signal": "bandwidth"
},
"extent": {
"signal": "trim ? null : [2000, 6500]"
}
}
]
},
{
"name": "stats",
"source": "penguins",
"transform": [
{
"type": "aggregate",
"groupby": [
"Species"
],
"fields": [
"Body Mass (g)",
"Body Mass (g)",
"Body Mass (g)"
],
"ops": [
"q1",
"median",
"q3"
],
"as": [
"q1",
"median",
"q3"
]
}
]
}
],
"scales": [
{
"name": "layout",
"type": "band",
"range": "width",
"domain": {
"data": "penguins",
"field": "Species"
}
},
{
"name": "yscale",
"type": "linear",
"range": "height",
"round": true,
"domain": {
"data": "penguins",
"field": "Body Mass (g)"
},
"domainMin": 2000,
"zero": false,
"nice": true
},
{
"name": "xscale",
"type": "linear",
"range": [
0,
{
"signal": "plotWidth"
}
],
"domain": {
"data": "density",
"field": "density"
}
},
{
"name": "color",
"type": "ordinal",
"domain": {
"data": "penguins",
"field": "Species"
},
"range": "category"
}
],
"axes": [
{
"orient": "left",
"scale": "yscale",
"zindex": 1
},
{
"orient": "bottom",
"scale": "layout",
"tickCount": 5,
"zindex": 1
}
],
"marks": [
{
"type": "group",
"from": {
"facet": {
"data": "density",
"name": "violin",
"groupby": "Species"
}
},
"encode": {
"enter": {
"xc": {
"scale": "layout",
"field": "Species",
"band": 0.5
},
"width": {
"signal": "plotWidth"
},
"height": {
"signal": "height"
}
}
},
"data": [
{
"name": "summary",
"source": "stats",
"transform": [
{
"type": "filter",
"expr": "datum.Species === parent.Species"
}
]
}
],
"marks": [
{
"type": "area",
"orient": "horizontal",
"from": {
"data": "violin"
},
"encode": {
"enter": {
"orient": {
"value": "horizontal"
},
"fill": {
"scale": "color",
"field": {
"parent": "Species"
}
}
},
"update": {
"y": {
"scale": "yscale",
"field": "value"
},
"xc": {
"signal": "plotWidth / 2"
},
"width": {
"scale": "xscale",
"field": "density"
}
}
}
},
{
"type": "rect",
"from": {
"data": "summary"
},
"encode": {
"enter": {
"fill": {
"value": "black"
},
"width": {
"value": 2
}
},
"update": {
"y": {
"scale": "yscale",
"field": "q1"
},
"y2": {
"scale": "yscale",
"field": "q3"
},
"xc": {
"signal": "plotWidth / 2"
}
}
}
},
{
"type": "rect",
"from": {
"data": "summary"
},
"encode": {
"enter": {
"fill": {
"value": "black"
},
"width": {
"value": 8
},
"height": {
"value": 2
}
},
"update": {
"y": {
"scale": "yscale",
"field": "median"
},
"xc": {
"signal": "plotWidth / 2"
}
}
}
}
]
}
]
}
@xverges
Copy link
Author

xverges commented Apr 7, 2021

Changing the https://vega.github.io/vega/examples/violin-plot/ orientation from horizontal to vertical.

Open the Chart in the Vega Editor

The main struggle has been learning how the orient horizontal needs to be set for an area vega/vega#3088

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment