Search code examples
chartsvega-lite

Filtering data on vegalite


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!


Solution

  • You can do this with a greater-than predicate:

    "transform": [{
      "filter": {
        "field": "Gross enrolment ratio for tertiary education both sexes (%)",
        "gt": 0
      }
    }],