Filtering data on vegalite - charts

I'm trying to filter my data so that it does not include the missing data (noted as 0) in my chart. I'd assume the code I need to add is
"transform": [
{"filter": "datum.Gross enrolment ratio for tertiary education both sexes (%) > 0"
}
],
but it doesn't seem to be working.
Here is the code for my vegalite chart
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"title": {
"text": "Billionaires and Education Attainment",
"subtitle": "Sources: ",
"subtitleFontStyle": "italic",
"subtitleFontSize": 10,
"anchor": "start",
"color": "black"
},
"height": 300,
"width": 260,
"data": {
"url": "https://raw.githubusercontent.com/jamieprince/jamieprince.github.io/main/correlation.csv"
},
"selection": {
"paintbrush": {
"type": "multi",
"on": "mouseover",
"nearest": true
},
"grid": {
"type": "interval",
"bind": "scales"
}
},
"mark": {
"type": "circle",
"opacity": 0.4,
"color": "skyblue"
},
"encoding": {
"x": {
"field": "Number of Billionaires",
"type": "quantitative",
"axis": {
"title": "Number of Billionaires",
"grid": false,
"tickCount": 14,
"labelOverlap": "greedy"
}
},
"y": {
"field": "Gross enrolment ratio for tertiary education both sexes (%)",
"type": "quantitative",
"axis": {
"title": "Educational Attainment",
"grid": false
}
},
"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": "Number of Billionaires",
"type": "nominal",
"title": "No of Billionaires"
},
{
"field": "Gross enrolment ratio for tertiary education both sexes (%)",
"type": "nominal",
"title": "Gross enrolment ratio for tertiary education both sexes (%)"
}
]
}
}
Does anyone know how I could filter the data so it only includes points where the variable is not 0?
Thank you so so much!

You can do this with a greater-than predicate:
"transform": [{
"filter": {
"field": "Gross enrolment ratio for tertiary education both sexes (%)",
"gt": 0
}
}],

Related

Adding a regression line on Vegalite

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"}
},

how to make an indented tree in vega

I'm trying to create an indented tree e.g. as in https://observablehq.com/#d3/indented-tree
I think that what this example does which I can't replicate in vega is encapsulated in this code:
root = { let i = 0; return d3.hierarchy(data).eachBefore(d => d.index = i++); }
eachBefore is a pre-order traversal on the output of d3.hierarchy.
Is there any way to get this result from (upstream) vega, or is this a feature request for an index output from the tree transform? (/something similar, or else a custom transform)
By the way, I think it may be easy to turn the specific tree layout example into an indented tree because the id happens to give the same 'index' ordering (I think), but think we need to use eachBefore where the data isn't so conveniently ordered.
Thanks for any suggestions!
Declan
Update
I made a change in vega-hierarchy described here:
https://github.com/declann/vega/commit/a651ff36cd3f0897054aa1b236f82e701db62432
Now I can use pre_traversal_id from tree transform output to do indented trees, e.g.:
indented tree in (custom) vega-editor, with tree output including pre_traversal_id field
Modified spec: https://gist.github.com/declann/91fd150ae04016e5890a30295fa58a07
Not sure if this help, but, when I enter at vega.github.io/vega/examples/tree-layout I played with the controls and (after change the settings to: layout:tidy - links:orthogonal - separation:true) I got a similar result you shown in the observablehq page:
Open the Chart in the Vega Editor
Code:
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An example of Cartesian layouts for a node-link diagram of hierarchical data.",
"width": 600,
"height": 1600,
"padding": 5,
"signals": [
{
"name": "labels", "value": true
},
{
"name": "layout", "value": "tidy"
},
{
"name": "links", "value": "orthogonal"
}
],
"data": [
{
"name": "tree",
"url": "data/flare.json",
"transform": [
{
"type": "stratify",
"key": "id",
"parentKey": "parent"
},
{
"type": "tree",
"method": {"signal": "layout", "value": "tidy"},
"size": [{"signal": "height"}, {"signal": "width - 100"}],
"separation": true,
"as": ["y", "x", "depth", "children"]
}
]
},
{
"name": "links",
"source": "tree",
"transform": [
{ "type": "treelinks" },
{
"type": "linkpath",
"orient": "horizontal",
"shape": {"signal": "links"}
}
]
}
],
"scales": [
{
"name": "color",
"type": "linear",
"range": {"scheme": "greys"},
"domain": {"data": "tree", "field": "depth"},
"zero": true
}
],
"marks": [
{
"type": "path",
"from": {"data": "links"},
"encode": {
"update": {
"path": {"field": "path"},
"stroke": {"value": "#828282"}
}
}
},
{
"type": "symbol",
"from": {"data": "tree"},
"encode": {
"enter": {
"size": {"value": 25}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"fill": {"field": "depth"}
}
}
},
{
"type": "text",
"from": {"data": "tree"},
"encode": {
"enter": {
"text": {"field": "name"},
"baseline": {"value": "middle"}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"dx": {"signal": "datum.children ? -7 : 7"},
"align": {"signal": "datum.children ? 'right' : 'left'"}
}
}
}
]
}
I believe this is possible by adding expression transforms for x (based on tree_node depth) and y (based on the tree node id)
The gist of it is to transform x and y after the tree transform
{"type": "stratify", "key": "id", "parentKey": "parent"},
{
"type": "tree",
"method": {"signal": "layout"},
"size": [{"signal": "height"}, {"signal": "width - 100"}],
"separation": {"signal": "separation"},
"as": ["_", "_", "depth", "children"]
},
{"type": "formula", "expr": "datum.depth * 20", "as": "x"},
{"type": "formula", "expr": "datum.id * 14", "as": "y"}
Here's an example that modifies the Vega Tree layout example

How to create Grouped Bar Chart in Vegalite?

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"}
}
}
}
]
}

How to change AmChart pie chart background?

How can I replace the color in the pie chart?
Please find my code below :
AmCharts.makeChart("pie-diagramm", {
"type": "pie",
"theme": "none",
"dataProvider": [{
"title": "Интернэшнл",
"value": 100
}, {
"title": "Казахстан",
"value": 90
},{
"title": "Лтд",
"value": 100
},
{
"title": "Лимитед",
"value": 400},
{
"title": "компания",
"value": 400},
{
"title": "ЛУКАРКО",
"value": 600},{
"title": "Компани",
"value": 700
},
{
"title": "«КазМунайГаз»",
"value": 900},{
"title": "КТК",
"value": 350},
{
"title": "Федерация",
"value": 1300},
{
"title": "Пайплайн",
"value": 90}
],
"titleField": "title",
"valueField": "value",
"labelRadius": 5,
"fontSize": 16,
"radius": "25%",
"innerRadius": "30%",
"labelText": "[[title]]",
"exportConfig": {
"menuItems": [{
"icon": '/lib/3/images/export.png',
"format": 'png'
}]
}
});
Thanks!
add color attribute in your DataProvider
dataProvider": [{
"title": "Интернэшнл",
"value": 100,
"color": "your choice of color"
}, {
"title": "Казахстан",
"value": 90,
"color": "your choice of color"
}
http://docs.amcharts.com/3/javascriptcharts/AmPieChart,
http://docs.amcharts.com/3/javascriptcharts/Slice
the dataprovider holds the property of each slice in pie chart
chart.chartData = dataProvider;
The best and most simple way to do this would be setting background-color style of your chart's container div. Or you can set chart.backgroundColor="#FF0000"; and chart.backgroundAlpha to some bigger than 0 value.

What rest API can be used to get default values of fields for create issue?

I am using jira rest api's in my application.
I have found the api for getting the meta-data for creating jira issue but that API doesn't return default values of the fields for example :-
This is the request :-
http://kelpie9:8081/rest/api/latest/issue/createmeta?projectKeys=QA&issuetypeNames=Bug&expand=project.issuetypes.fields
the default value of priority field is set to "major" and the description of priority is also customized but the return from api is:-
{
"expand": "projects",
"projects": [
{
"expand": "issuetypes",
"self": "http://kelpie9:8081/rest/api/2/project/QA",
"id": "10010",
"key": "QA",
"name": "QA",
"avatarUrls": {
"16x16": "http://kelpie9:8081/secure/projectavatar?size=small&pid=10010&avatarId=10011",
"48x48": "http://kelpie9:8081/secure/projectavatar?pid=10010&avatarId=10011"
},
"issuetypes": [
{
"expand": "fields",
"self": "http://kelpie9:8081/rest/api/2/issuetype/1",
"id": 1,
"name": "Bug",
"iconUrl": "http://kelpie9:8081/images/icons/bug.gif",
"fields": {
"summary": {
"required": true,
"schema": {
"type": "string",
"system": "summary"
},
"operations": [
"set"
]
},
"timetracking": {
"required": false,
"operations": [ ]
},
"issuetype": {
"required": true,
"schema": {
"type": "issuetype",
"system": "issuetype"
},
"operations": [ ],
"allowedValues": [
{
"id": "1",
"name": "Bug",
"description": "A problem which impairs or prevents the functions of the product.",
"iconUrl": "http://kelpie9:8081/images/icons/bug.gif"
}
]
},
"priority": {
"required": false,
"schema": {
"type": "priority",
"system": "priority"
},
"name": "Priority",
"operations": [
"set"
],
"allowedValues": [
{
"self": "http://172.19.30.101:18080/rest/api/2/priority/1",
"iconUrl": "http://172.19.30.101:18080/images/icons/priority_blocker.gif",
"name": "Blocker",
"id": "1"
},
{
"self": "http://172.19.30.101:18080/rest/api/2/priority/2",
"iconUrl": "http://172.19.30.101:18080/images/icons/priority_critical.gif",
"name": "Critical",
"id": "2"
},
{
"self": "http://172.19.30.101:18080/rest/api/2/priority/3",
"iconUrl": "http://172.19.30.101:18080/images/icons/priority_major.gif",
"name": "Major",
"id": "3"
},
{
"self": "http://172.19.30.101:18080/rest/api/2/priority/4",
"iconUrl": "http://172.19.30.101:18080/images/icons/priority_minor.gif",
"name": "Minor",
"id": "4"
},
{
"self": "http://172.19.30.101:18080/rest/api/2/priority/5",
"iconUrl": "http://172.19.30.101:18080/images/icons/priority_trivial.gif",
"name": "Trivial",
"id": "5"
}
]
},
"customfield_10080": {
"required": false,
"schema": {
"type": "array",
"items": "string",
"custom": "com.atlassian.jira.plugin.system.customfieldtypes:labels",
"customId": 10080
},
"operations": [ ]
},
"customfield_10010": {
"required": false,
"schema": {
"type": "array",
"items": "string",
"custom": "com.atlassian.jira.plugin.system.customfieldtypes:labels",
"customId": 10010
},
"operations": [ ]
},
"customfield_10071": {
"required": false,
"schema": {
"type": "array",
"items": "string",
"custom": "com.atlassian.jira.plugin.system.customfieldtypes:textfield",
"customId": 10071
},
"operations": [ ]
}
}
}
]
}
]
}
There is nothing like default value or description in priority field, how will I get those values?