dara keeley
dara keeley

Reputation: 19

How to do I alter code so chart will display?

I have created an interactive sorted bar chart using an online vega lite editor (https://vega.github.io/editor/#) however my chart will not display despite running through each line and checking syntax. Need some help please!

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "data": {
    "url": "https://raw.githubusercontent.com/dkeeley/dkeeley/master/Happiness_2019.csv",
    "format": {
      "type": "csv"
    }
  },
  "transform": [
    {
      "filter": "datum.Score > 0"
   },
   {
      "filter": "datum.GDP_per_capita > 0"
    },
    {
      "filter": "indexof(datum.Country_or_region, ',') > 0"
    }
  ],  
 "repeat": {"column": ["Score","GDP_per_capita"]},
 "spec":{
    "selection": {
        "Country_or_region_selection": {"type": "multi", "fields": ["Country_or_region"]}
      },
    "mark": "bar",
    "encoding": {
        "y": {
        "field": "Country_or_region",
        "type": "nominal",
        "axis": {"title": null},
        "sort": {
            "op": "sum",
            "field": {"repeat": "column"},
            "order": "descending"
        }
        },
        "x": {
        "field": {"repeat": "column"},
        "type": "quantitative",
        "aggregate": "sum"
        },
        "color": {
          "condition": {
            "selection": "Country_or_region_selection",
            "field": "Country_or_region",
            "type":"nominal",
            "legend":null
            },
          "value": "grey"    
        }
    }
  }
}

Upvotes: 1

Views: 67

Answers (1)

jakevdp
jakevdp

Reputation: 86310

It looks like your data is a "tsv" rather than a "csv", and your fields have spaces rather than underscores (i.e. datum['Country or region'], not datum.Country_or_region):

(view in editor)

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "data": {
    "url": "https://raw.githubusercontent.com/dkeeley/dkeeley/master/Happiness_2019.csv",
    "format": {
      "type": "tsv"
    }
  },
  "transform": [
    {
      "filter": "datum.Score > 0"
   },
   {
      "filter": "datum['GDP per capita'] > 0"
    }
  ],  
 "repeat": {"column": ["Score","GDP per capita"]},
 "spec":{
    "selection": {
        "Country_or_region_selection": {"type": "multi", "fields": ["Country or region"]}
      },
    "mark": "bar",
    "encoding": {
        "y": {
        "field": "Country or region",
        "type": "nominal",
        "axis": {"title": null},
        "sort": {
            "op": "sum",
            "field": {"repeat": "column"},
            "order": "descending"
        }
        },
        "x": {
        "field": {"repeat": "column"},
        "type": "quantitative",
        "aggregate": "sum"
        },
        "color": {
          "condition": {
            "selection": "Country_or_region_selection",
            "field": "Country or region",
            "type":"nominal",
            "legend":null
            },
          "value": "grey"    
        }
    }
  }
}

Upvotes: 1

Related Questions