The vega documentation/tutorials suggest that setting up a horizontal bar chart is easy, but I can't figure it out for the life of me. How would I go about changing a vertical bar chart, such as http://vega.github.io/vega-editor/index.html?spec=bar, to a horizontal one?
Like this
{
"width": 200,
"height": 210,
"padding": "auto",
"data": [
{
"name": "raw",
"values": [
{"x": "A","y": 28},
{"x": "B","y": 55},
{"x": "C","y": 43},
{"x": "D","y": 91},
{"x": "E","y": 81},
{"x": "F","y": 53},
{"x": "G","y": 19},
{"x": "H","y": 87},
{"x": "I","y": 52}
],
"format": {"parse": {"y": "number"}},
"transform": [{"type": "filter","test": "(d.data.y!==null)"}]
}
],
"scales": [],
"marks": [
{
"_name": "cell",
"type": "group",
"properties": {
"enter": {"width": {"value": 200},"height": {"value": 210}}
},
"scales": [
{
"name": "x",
"type": "linear",
"domain": {"data": "raw","field": "data.y"},
"range": [0,200],
"zero": true,
"reverse": false,
"round": true,
"nice": true
},
{
"name": "y",
"type": "ordinal",
"domain": {"data": "raw","field": "data.x"},
"sort": true,
"range": [0,210],
"bandWidth": 21,
"round": true,
"nice": true,
"points": true,
"padding": 1
}
],
"axes": [
{
"type": "x",
"scale": "x",
"properties": {
"grid": {
"stroke": {"value": "#000000"},
"opacity": {"value": 0.08}
}
},
"layer": "back",
"format": "",
"ticks": 5,
"titleOffset": 38,
"grid": true,
"title": "y"
},
{
"type": "y",
"scale": "y",
"properties": {
"labels": {"text": {"template": "{{data | truncate:25}}"}},
"grid": {
"stroke": {"value": "#000000"},
"opacity": {"value": 0.08}
}
},
"layer": "back",
"titleOffset": 28,
"grid": true,
"title": "x"
}
],
"marks": [
{
"type": "rect",
"from": {"data": "raw"},
"properties": {
"enter": {
"x": {"scale": "x","field": "data.y"},
"x2": {"value": 0},
"yc": {"scale": "y","field": "data.x"},
"height": {"value": 21,"offset": -1},
"fill": {"value": "#4682b4"}
},
"update": {
"x": {"scale": "x","field": "data.y"},
"x2": {"value": 0},
"yc": {"scale": "y","field": "data.x"},
"height": {"value": 21,"offset": -1},
"fill": {"value": "#4682b4"}
}
}
}
],
"legends": []
}
]
}
I generated this chart from
{
"data": {
"values": [
{"x":"A", "y":28}, {"x":"B", "y":55}, {"x":"C", "y":43},
{"x":"D", "y":91}, {"x":"E", "y":81}, {"x":"F", "y":53},
{"x":"G", "y":19}, {"x":"H", "y":87}, {"x":"I", "y":52}
]
},
"marktype": "bar",
"encoding": {
"y": {"type": "O","name": "x"},
"x": {"type": "Q","name": "y"}
}
}
with vega-lite (https://vega.github.io/vega-lite/).
Related
How do I convert number to double word in Node-red?
I tried throwing the array in
but no response
I'm not very good at node red, please answer me, thank you
What do you mean converting 'number' to 'double word'?
Data type 'double' is already number type.
Do you mean String to number?
[
{
"id": "bd8884d5c2f82322",
"type": "tab",
"label": "플로우 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "4546977fe7434e95",
"type": "inject",
"z": "bd8884d5c2f82322",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "1234.34",
"payloadType": "str",
"x": 160,
"y": 40,
"wires": [
[
"a93489fbd55883b2",
"f1ad7ec076bceb78"
]
]
},
{
"id": "a93489fbd55883b2",
"type": "function",
"z": "bd8884d5c2f82322",
"name": "Convert String to Number",
"func": "msg.payload=Number(msg.payload);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 370,
"y": 40,
"wires": [
[
"1f695a58ebc55f90"
]
]
},
{
"id": "1f695a58ebc55f90",
"type": "debug",
"z": "bd8884d5c2f82322",
"name": "number",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 560,
"y": 40,
"wires": []
},
{
"id": "f1ad7ec076bceb78",
"type": "debug",
"z": "bd8884d5c2f82322",
"name": "string",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 310,
"y": 80,
"wires": []
}]
I'm trying to add a regression line and R-squared value on my scatterplot. I know I'm supposed to use the layer function and
"transform": [
{
"regression": "GDP per capita",
"on": "Educationalattainment",
}
but after trying a million times I can't figure out where to insert the line of codes. This is the code for my chart
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"title": {
"text": "GDP per capita and Education Attainment",
"subtitle": "From 2015-2020. Sources: World Bank",
"subtitleFontStyle": "italic",
"subtitleFontSize": 10,
"anchor": "start",
"color": "black"
},
"height": 300,
"width": 300,
"data": {
"url": "https://raw.githubusercontent.com/jamieprince/jamieprince.github.io/main/correlation.csv"
},
"transform": [
{"calculate": "datum.Educationalattainment/100", "as": "percent"},
{"filter": {
"field": "Educationalattainment",
"gt": 0
}}
],
"selection": {
"paintbrush": {
"type": "multi",
"on": "mouseover",
"nearest": true
},
"grid": {
"type": "interval",
"bind": "scales"
}
},
"mark": {
"type": "circle",
"opacity": 0.5,
"color": "#EC9D3E"
},
"encoding": {
"x": {
"field": "GDP per capita",
"type": "quantitative",
"axis": {
"title": "GDP per capita",
"grid": false,
"tickCount": 10,
"labelOverlap": "greedy"
}
},
"y": {
"field": "percent",
"type": "quantitative",
"axis": {
"title": "Educational Attainment",
"grid": false, "format":"%"
}
},
"size": {
"condition": {
"selection": "paintbrush",
"value": 300,
"init": {
"value": 70
}
},
"value": 70
},
"tooltip": [
{
"field": "Year",
"type": "nominal",
"title": "Year"
},
{
"field": "Country",
"type": "ordinal",
"title": "Country"
},
{
"field": "GDP per capita",
"type": "nominal",
"title": "GDP per capita"
},
{
"field": "Educationalattainment",
"type": "nominal",
"title": "Educational attainment at least completed short-cycle tertiary population 25+ total (%) (cumulative)"
}
]
}
}
And this is my reference chart
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "Figure 5: Plotting a regression of Social Mobility Index on Global Entrepreneurship Index, equation acquired via Python",
"data": {
"url": "https://raw.githubusercontent.com/marinabrts/marinabrts.github.io/main/GEIxSMI.csv",
"format": {"type": "csv"}
},
"background": "#E0E0E0",
"config": {"axis": {"grid": true, "gridColor": "#FFFFFF"}},
"title": {
"text": "Figure 5: Regressing SMI on Global Entrepreneurship Index",
"subtitle": "Source: World Economic Forum (2020), Global Entrepreneurship & Development Institute (2019)",
"subtitleFontStyle": "italic",
"subtitleFontSize": 10,
"anchor": "start"
},
"height": 300,
"width": 370,
"layer": [
{
"mark": {"type": "point", "size": 30, "color": "#FF3399"},
"encoding": {
"x": {
"field": "GEI",
"type": "quantitative",
"title": "Global Entrepreneurship Index (GEI)"
},
"y": {
"field": "Index Score",
"type": "quantitative",
"title": "Social Mobility Index (SMI)",
"scale": {"domain": [30, 90]}
},
"tooltip": [
{"field": "Country", "type": "nominal", "title": "Country"},
{"field": "GEI", "type": "quantitative", "title": "GEI"},
{"field": "Index Score", "type": "quantitative", "title": "SMI"}
]
}
},
{
"mark": {"type": "line", "color": "#7F00FF", "size": 3},
"transform": [{"regression": "Index Score", "on": "GEI"}],
"encoding": {
"x": {"field": "GEI", "type": "quantitative"},
"y": {"field": "Index Score", "type": "quantitative"}
}
},
{
"transform": [
{"regression": "Index Score", "on": "GEI", "params": true},
{"calculate": "'R²= '+format(datum.rSquared, '.2f')", "as": "R2"}
],
"mark": {
"type": "text",
"color": "red",
"size": 14,
"x": "width",
"align": "center",
"y": -5
},
"encoding": {"text": {"type": "nominal", "field": "R2"}}
}
]
}
I'd be so so grateful for any help. Thank you!
Edit:
Code for R-squared value
{
"transform": [
{
"regression": "GDP per capita",
"on": "percent",
"params": true
},
{"calculate": "'R²: '+format(datum.rSquared, '.2f')", "as": "R2"}
],
"mark": {
"type": "text",
"color": "black",
"x": "width",
"align": "right",
"y": -5
},
"encoding": {
"text": {"type": "nominal", "field": "R2"}
}
}
Complete chart code where the value does not appear
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"title": {
"text": null,
"subtitle": null,
"subtitleFontStyle": "italic",
"subtitleFontSize": 10,
"anchor": "start",
"color": "black"
},
"height": 100,
"width": 100,
"data": {
"url": "https://raw.githubusercontent.com/jamieprince/jamieprince.github.io/main/correlation.csv"
},
"transform": [
{"calculate": "datum.Educationalattainment/100", "as": "percent"},
{"filter": {"field": "Educationalattainment", "gt": 0}}
],
"layer": [
{
"selection": {
"paintbrush": {"type": "multi", "on": "mouseover", "nearest": true},
"grid": {"type": "interval", "bind": "scales"}
},
"mark": {"type": "circle", "opacity": 0.5, "color": "#EC9D3E"},
"encoding": {
"x": {
"field": "GDP per capita",
"type": "quantitative",
"axis": {
"title": "GDP per capita",
"grid": false,
"tickCount": 10,
"labelOverlap": "greedy"
}
},
"y": {
"field": "percent",
"type": "quantitative",
"axis": {
"title": "Educational Attainment",
"grid": false,
"format": "%"
}
},
"size": {
"condition": {
"selection": "paintbrush",
"value": 300,
"init": {"value": 70}
},
"value": 70
},
"tooltip": [
{"field": "Year", "type": "nominal", "title": "Year"},
{"field": "Country", "type": "ordinal", "title": "Country"},
{
"field": "GDP per capita",
"type": "nominal",
"title": "GDP per capita"
},
{
"field": "Educationalattainment",
"type": "nominal",
"title": "Educational attainment at least completed short-cycle tertiary population 25+ total (%) (cumulative)"
}
]
}
},
{
"mark": {"type": "line", "color": "#347DB6", "size": 3},
"transform": [{"regression": "GDP per capita", "on": "percent"}],
"encoding": {
"x": {"field": "GDP per capita", "type": "quantitative"},
"y": {"field": "percent", "type": "quantitative"}
}
},
{
"transform": [
{
"regression": "GDP per capita",
"on": "percent",
"params": true
},
{"calculate": "'R²: '+format(datum.rSquared, '.2f')", "as": "R2"}
],
"mark": {
"type": "text",
"color": "black",
"x": "width",
"align": "right",
"y": -5
},
"encoding": {
"text": {"type": "nominal", "field": "R2"}
}
}
]
}
You need to simply add the scatter chart and a line mark inside a layer which will be stacked on top of each other. Then, perform regression transform on the line mark. The transform which is provided in your question seems wrong, as there is no x or y field having Educationalattainment, so I did the regression on percent field as it was calculated and derived from the Educationalattainment field:
"transform": [
{
"regression": "GDP per capita",
"on": "Educationalattainment",
}]
Below is the modified config or refer editor:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"title": {
"text": null,
"subtitle": null,
"subtitleFontStyle": "italic",
"subtitleFontSize": 10,
"anchor": "start",
"color": "black"
},
"height": 100,
"width": 100,
"data": {
"url": "https://raw.githubusercontent.com/jamieprince/jamieprince.github.io/main/correlation.csv"
},
"transform": [
{"calculate": "datum.Educationalattainment/100", "as": "percent"},
{"filter": {"field": "Educationalattainment", "gt": 0}}
],
"layer": [
{
"selection": {
"paintbrush": {"type": "multi", "on": "mouseover", "nearest": true},
"grid": {"type": "interval"}
},
"mark": {"type": "circle", "opacity": 0.5, "color": "#EC9D3E"},
"encoding": {
"x": {
"field": "GDP per capita",
"type": "quantitative",
"axis": {
"title": "GDP per capita",
"grid": false,
"tickCount": 10,
"labelOverlap": "greedy"
}
},
"y": {
"field": "percent",
"type": "quantitative",
"axis": {
"title": "Educational Attainment",
"grid": false,
"format": "%"
}
},
"size": {
"condition": {
"selection": "paintbrush",
"value": 300,
"init": {"value": 70}
},
"value": 70
},
"tooltip": [
{"field": "Year", "type": "nominal", "title": "Year"},
{"field": "Country", "type": "ordinal", "title": "Country"},
{
"field": "GDP per capita",
"type": "nominal",
"title": "GDP per capita"
},
{
"field": "Educationalattainment",
"type": "nominal",
"title": "Educational attainment at least completed short-cycle tertiary population 25+ total (%) (cumulative)"
}
]
}
},
{
"mark": {"type": "line", "color": "#347DB6", "size": 3},
"transform": [{"regression": "GDP per capita", "on": "percent"}],
"encoding": {
"x": {"field": "GDP per capita", "type": "quantitative"},
"y": {"field": "percent", "type": "quantitative"}
}
},
{
"transform": [
{"regression": "GDP per capita", "on": "percent", "params": true},
{"calculate": "'R²: '+format(datum.rSquared, '.2f')", "as": "R2"}
],
"mark": {
"type": "text",
"color": "black",
"x": "width",
"align": "right",
"y": -5
},
"encoding": {"text": {"type": "nominal", "field": "R2"}}
}
]
}
Edit
To show the text, I have removed the bind config from your grid selection. After removing it the text was properly visible, this can be an issue or there will be some reason behind it.
Updated the following line in above snippet:
"selection": {
"paintbrush": {"type": "multi", "on": "mouseover", "nearest": true},
"grid": {"type": "interval"}
},
Name | Value 1 | Value 2
BTC | 1 | 2
ETH | 1 | 2
to this:
Tried to used this as an example: https://vega.github.io/vega-lite/examples/bar_grouped.html,
but I can't make it work.
Can someone please point me to the right direction? Thank you in advance.
Instead of using column provided in your question, You can simply use layers and keep the x axis as common and provide value1 and value2 in y axis of each layer respectively and simply provide some offset to show it as a grouped bar chart. Below is the basic spec configuration and editor:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple bar chart with embedded data.",
"title": "My chart",
"width": 200,
"data": {
"values": [
{"name": "BTH", "value1": 28, "value2": 24, "legendTitle": "value1"},
{"name": "ETH", "value1": 55, "value2": 25, "legendTitle": "value2"}
]
},
"encoding": {
"x": {"field": "name", "type": "nominal", "axis": {"labelAngle": 0}}
},
"layer": [
{
"mark": {"type": "bar", "xOffset": -20, "size": 30, "color": "skyblue"},
"encoding": {
"y": {
"field": "value1",
"type": "quantitative",
"axis": {"title": null, "ticks": false}
}
}
},
{
"mark": {"type": "bar", "size": 30, "xOffset": 18, "color": "orange"},
"encoding": {
"y": {
"field": "value2",
"type": "quantitative",
"axis": {"title": null, "ticks": false}
}
}
},
{
"mark": {"type": "text"},
"encoding": {
"fill": {
"field": "legendTitle",
"scale": {"range": ["skyBlue", "orange"]},
"legend": {"title": null, "symbolType": "square", "orient": "bottom"}
}
}
}
]
}
I am looking to create this kind of chart in Vega:
I carefully read the documentation about marks here:
https://vega.github.io/vega/docs/marks/line/
I read about the Type-Specific Mark Properties and about the defined property and it seemed like what I need. But I have no idea how to use this property.
I have my marks defined like so:
'marks': [
{
'name': 'expected_sales',
'description': 'The sales line',
'type': 'line',
'defined': 'false', // this I added based on the documentation
'from': {
'data': 'SalesData'
},
'zindex': 100,
'encode': { ... }
}
]
But this apparently doesn't work. the line is still continued. I have to mention that the data points that I get don't have null values, but 0.0.
Considering that sales might be $0 at some point, it is better to deferentiate between 0 values and null values.
That said, because null values are defined as 0.0 in the dataset, defined property has to be true for all other points except when value is 0.0
In the below example, "defined": {"signal": "datum.v !== 0.0"} is used to conditionally assign "defined" property to false if value "datum.v" is 0.0
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 400,
"height": 200,
"padding": 5,
"data": [
{
"name": "table",
"values": [
{"u": 1, "v": 28}, {"u": 2, "v": 12.0},
{"u": 3, "v": 0.0}, {"u": 4, "v": 10},
{"u": 5, "v": 36}, {"u": 6, "v": 44}
]
}
],
"scales": [
{
"name": "xscale",
"type": "linear",
"range": "width",
"zero": false,
"domain": {"data": "table", "field": "u"}
},
{
"name": "yscale",
"type": "linear",
"range": "height",
"nice": true,
"zero": false,
"domain": {"data": "table", "field": "v"}
}
],
"axes": [
{"scale": "xscale", "orient": "bottom", "grid": true},
{"scale": "yscale", "orient": "left"}
],
"marks": [
{
"type": "line",
"from": {"data": "table"},
"encode": {
"enter": {
"stroke": {"value": "#652c90"}
},
"update": {
"x": {"scale": "xscale", "field": "u"},
"y": {"scale": "yscale", "field": "v"},
"defined": {"signal": "datum.v !== 0.0"},
"interpolate": {"value": "linear"},
"strokeWidth": {"value": 4},
"strokeDash": {"value": [1,0]},
"strokeCap": {"value": "square"},
"opacity": {"value": 1}
},
"hover": {
"opacity": {"value": 0.5}
}
}
}
]
}
i whant to make every bar in chart - in different collor. please help.
so default chart like described in docs:
https://vega.github.io/vega/examples/bar-chart/
for example - i whant to make like: blue, red, yellow, green, blue, blue.
please help! thanks!
Using the Vega bar chart example you linked to:
First, create a scale to define the colors:
{
"name": "colors",
"type": "ordinal",
"domain": {"data": "table", "field": "category"},
"range": {"scheme": "category10"}
}
The value for the "scheme" can be a built-in scheme from Vega, or a custom scheme.
Then, you need to specify the color for the fill on the mark's update property set, specifying the name of the scheme:
"update": {
"fill": {"scale": "colors", "field": "category"}
},
You can define a categorical color scale and use it to change the color of the bar mark. Check out some of the other examples that use categorical color scales.
I had the same problem and with a little help from https://vega.github.io/vega-lite/examples/bar_color_disabled_scale.html I got to the following - the idea is to encode the color in the data:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": "A", "b": 28, "c": "black"},
{"a": "B", "b": 55, "c": "black"},
{"a": "C", "b": 43, "c": "black"},
{"a": "D", "b": 91, "c": "red"}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "ordinal"},
"y": {"field": "b", "type": "quantitative"},
"color": {"field": "c", "type": "nominal", "scale": null}
}
}
alternatively this also works and keeps the legend if you need it:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": "A", "b": 28},
{"a": "B", "b": 55},
{"a": "C", "b": 43},
{"a": "D", "b": 91}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "ordinal", "sort": ["C", "A", "B", "D"]},
"y": {"field": "b", "type": "quantitative"},
"color": {"field": "a", "type": "nominal", "sort": ["C", "A", "B", "D"], "scale": {"range": ["red", "black", "black", "black", "black"]}}
}
}
For bar charts
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A bar chart that directly encodes color names in the data.",
"data": {
"values": [
{
"color": "yellow",
"b": 28
},
{
"color": "green",
"b": 55
},
{
"color": "blue",
"b": 43
}
]
},
"mark": "bar",
"encoding": {
"x": {
"field": "color",
"type": "nominal"
},
"y": {
"field": "b",
"type": "quantitative"
},
"color": {
"field": "color",
"type": "nominal",
"scale": null
}
}
}
For Pie charts
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 200,
"height": 200,
"autosize": "none",
"signals": [
{
"name": "startAngle", "value": 0,
"bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
},
{
"name": "endAngle", "value": 6.29,
"bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
},
{
"name": "padAngle", "value": 0,
"bind": {"input": "range", "min": 0, "max": 0.1}
},
{
"name": "innerRadius", "value": 0,
"bind": {"input": "range", "min": 0, "max": 90, "step": 1}
},
{
"name": "cornerRadius", "value": 0,
"bind": {"input": "range", "min": 0, "max": 10, "step": 0.5}
},
{
"name": "sort", "value": false,
"bind": {"input": "checkbox"}
}
],
"data": [
{
"name": "table",
"values": [
{"field": 10,"color": "blue"},
{"field": 3,"color": "green"},
{"field": 7,"color": "red"},
{"field": 8,"color":"orange"},
{"field": 15,"color":"yellow"}
],
"transform": [
{
"type": "pie",
"field": "field",
"startAngle": {"signal": "startAngle"},
"endAngle": {"signal": "endAngle"},
"sort": {"signal": "sort"}
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "table", "field": "color"},
"range": {"scheme": "category20"}
}
],
"marks": [
{
"type": "arc",
"from": {"data": "table"},
"encode": {
"enter": {
"fill": { "field": "color",
"type": "nominal",
"scale": null},
"x": {"signal": "width / 2"},
"y": {"signal": "height / 2"}
},
"update": {
"startAngle": {"field": "startAngle"},
"endAngle": {"field": "endAngle"},
"padAngle": {"signal": "padAngle"},
"innerRadius": {"signal": "innerRadius"},
"outerRadius": {"signal": "width / 2"},
"cornerRadius": {"signal": "cornerRadius"}
}
}
}
]
}