vega-litevegavega-embed

How can i used different field for label(y.axis.label) on y axis and different field for encoding (y.field) in Vegalite


In Vegalite, I want to display label on y axis on the basis of specific field(y_field_name) and encoding calculation y value is different field(y_axis_field) .

example :-- in the provided editor link i use "y_axis_field" for y axis field but i want to use field name "y_field_name" for the label expression, such that y axis use one data field for mapping the values with the another axis and another field to show the label-expression.. basically i want to use two fields for y axis ie one for using their values and second for label expression.. How this thing can be possible in vegalite..

Please help me. Thanks in advance..


Solution

  • Here I cancat the values and split them after. It's a trick I use quite often since labels are limited to the axis value.

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "description": "A simple bar chart with embedded data.",
      "data": {
        "values": [
          {"y_axis_field": "A", "b": 28, "y_field_name": "P"},
          {"y_axis_field": "B", "b": 55, "y_field_name": "Q"},
          {"y_axis_field": "C", "b": 43, "y_field_name": "R"},
          {"y_axis_field": "D", "b": 91, "y_field_name": "S"},
          {"y_axis_field": "E", "b": 81, "y_field_name": "T"},
          {"y_axis_field": "F", "b": 53, "y_field_name": "U"},
          {"y_axis_field": "G", "b": 19, "y_field_name": "V"},
          {"y_axis_field": "H", "b": 87, "y_field_name": "W"},
          {"y_axis_field": "I", "b": 52, "y_field_name": "X"}
        ]
      },
      "transform": [
        {
          "calculate": "datum.y_axis_field + '|' + datum.y_field_name",
          "as": "y_axis_full_descr"
        }
      ],
      "mark": "point",
      "encoding": {
        "x": {"field": "b", "type": "nominal", "axis": {"labelAngle": 0}},
        "y": {
          "field": "y_axis_full_descr",
          "type": "nominal",
          "title": "",
          "axis": {"labelExpr": "split(datum.label, '|')[1]"}
        }
      }
    }