自定义vega-lite线图轴标签

时间:2018-11-12 15:37:44

标签: vega-lite

我想做的事情确实很简单,但是我似乎做对了。我觉得答案会让我感到尴尬!

我有一个折线图,x轴为“尝试”,y轴为“坡度”,坡度为0到100之间的数字。我只想更改y轴,以代替看到原始数字时,将显示等级,例如0-20代表“ E”,20-40代表“ D”,以此类推,直到“ A”(80-100)。我怎样才能做到这一点?我不想使用离散值,因为我想直观地显示成绩范围内的等级。我不确定我是否只是想在成绩单上显示成绩等级还是将其放在刻度线的中间,但是仅此而已会很有帮助!

这是我在vega-lite编辑器中一直在使用的内容:



    {
      "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
      "data": {
        "values": [
          {
            "attempt": 1,
            "score": 30
          },
          {
            "attempt": 2,
            "score": 60
          },
          {
            "attempt": 3,
            "score": 75
          },
          {
            "attempt": 4,
            "score": 58
          },
          {
            "attempt": 5,
            "score": 67
          }
        ]
      },
      "mark": {
        "type": "line",
        "color": "#22bc9a",
        "point": {
          "filled": false
        }
      },
      "encoding": {
        "x": {
          "field": "attempt",
          "type": "quantitative",
          "axis": {
            "grid": false,
            "tickCount": 5,
            "title": "Attempt"
          }
        },
        "y": {
          "field": "score",
          "scale": {"domain": [0, 100]},
          "type": "quantitative",
          "axis": {
            "tickCount": 5,
            "title": "Grade"
          }
        },
        "opacity": {"value": 0.3}
      },
      "config": {
        "autosize": "fit",
        "axis": {
          "labelColor": "#bebec8",
          "tickColor": "#bebec8",
          "titleColor": "black",
          "titleFontWeight": "normal",
          "titleFontSize": 11,
          "labelFont": "Helvetica",
          "titleFont": "Helvetica",
          "gridOpacity": 0.4,
          "gridWidth": 1.5,
          "domain": false
        },
        "view": {
          "strokeWidth": 0
        }
      }
    }

谢谢。

2 个答案:

答案 0 :(得分:1)

这样的事情呢:我添加了一个具有等级类别的数据框,并使用它来叠加一些文本。我删除了轴的标签,因此文本的行为就好像是轴的标签。

图表看起来像这样,here is a link to the editorchart_with_grades_for_axis

模式(请注意,我是使用Python的Altair完成的,因此它可能不是规范的Vega-lite,并且我也没有使用您的设置,对此感到抱歉):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.6.0.json",
  "config": {
    "view": {
      "height": 300,
      "width": 400
    }
  },
  "datasets": {
    "data-1acee7c5d817865a529b53e022474ce8": [
      {
        "label": "E",
        "x_min": 1,
        "y_med": 10
      },
      {
        "label": "D",
        "x_min": 1,
        "y_med": 30
      },
      {
        "label": "C",
        "x_min": 1,
        "y_med": 50
      },
      {
        "label": "B",
        "x_min": 1,
        "y_med": 70
      },
      {
        "label": "A",
        "x_min": 1,
        "y_med": 90
      }
    ],
    "data-8e6359ea3034b8410708361bb10fafd5": [
      {
        "attempt": 1,
        "score": 30
      },
      {
        "attempt": 2,
        "score": 60
      },
      {
        "attempt": 3,
        "score": 75
      },
      {
        "attempt": 4,
        "score": 58
      },
      {
        "attempt": 5,
        "score": 67
      }
    ]
  },
  "layer": [
    {
      "data": {
        "name": "data-1acee7c5d817865a529b53e022474ce8"
      },
      "encoding": {
        "text": {
          "field": "label",
          "type": "ordinal"
        },
        "x": {
          "field": "x_min",
          "scale": {
            "zero": false
          },
          "type": "quantitative"
        },
        "y": {
          "axis": {
            "labels": false,
            "tickCount": 5,
            "ticks": false
          },
          "field": "y_med",
          "type": "quantitative"
        }
      },
      "mark": {
        "dx": -15,
        "dy": 8,
        "size": 15,
        "type": "text"
      }
    },
    {
      "data": {
        "name": "data-8e6359ea3034b8410708361bb10fafd5"
      },
      "encoding": {
        "x": {
          "axis": {
            "tickCount": 5
          },
          "field": "attempt",
          "title": "Attempt",
          "type": "quantitative"
        },
        "y": {
          "field": "score",
          "scale": {
            "domain": [
              0,
              100
            ]
          },
          "title": "Grade",
          "type": "quantitative"
        }
      },
      "mark": {
        "point": true,
        "type": "line"
      }
    }
  ]
}

使用类别的略微修改的数据框(添加了x_maxy_miny_max),您可以添加带有彩色矩形的另一层,这可以帮助读取值: chart_with_grade_axis_and_color_bands

这里是link to the editor

这是架构

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.6.0.json",
  "config": {
    "view": {
      "height": 300,
      "width": 400
    }
  },
  "datasets": {
    "data-1acee7c5d817865a529b53e022474ce8": [
      {
        "label": "E",
        "x_min": 1,
        "y_med": 10
      },
      {
        "label": "D",
        "x_min": 1,
        "y_med": 30
      },
      {
        "label": "C",
        "x_min": 1,
        "y_med": 50
      },
      {
        "label": "B",
        "x_min": 1,
        "y_med": 70
      },
      {
        "label": "A",
        "x_min": 1,
        "y_med": 90
      }
    ],
    "data-39ffbda2b5d5fe96de84d9e308d920ff": [
      {
        "label": "E",
        "x_max": 5,
        "x_min": 1,
        "y_max": 20,
        "y_med": 10,
        "y_min": 0
      },
      {
        "label": "D",
        "x_max": 5,
        "x_min": 1,
        "y_max": 40,
        "y_med": 30,
        "y_min": 20
      },
      {
        "label": "C",
        "x_max": 5,
        "x_min": 1,
        "y_max": 60,
        "y_med": 50,
        "y_min": 40
      },
      {
        "label": "B",
        "x_max": 5,
        "x_min": 1,
        "y_max": 80,
        "y_med": 70,
        "y_min": 60
      },
      {
        "label": "A",
        "x_max": 5,
        "x_min": 1,
        "y_max": 100,
        "y_med": 90,
        "y_min": 80
      }
    ],
    "data-8e6359ea3034b8410708361bb10fafd5": [
      {
        "attempt": 1,
        "score": 30
      },
      {
        "attempt": 2,
        "score": 60
      },
      {
        "attempt": 3,
        "score": 75
      },
      {
        "attempt": 4,
        "score": 58
      },
      {
        "attempt": 5,
        "score": 67
      }
    ]
  },
  "layer": [
    {
      "data": {
        "name": "data-39ffbda2b5d5fe96de84d9e308d920ff"
      },
      "encoding": {
        "color": {
          "field": "label",
          "scale": {
            "scheme": "greenblue"
          },
          "type": "ordinal"
        },
        "x": {
          "field": "x_min",
          "scale": {
            "zero": false
          },
          "title": "Attempt",
          "type": "quantitative"
        },
        "x2": {
          "field": "x_max",
          "type": "quantitative"
        },
        "y": {
          "axis": null,
          "field": "y_min",
          "type": "quantitative"
        },
        "y2": {
          "field": "y_max",
          "type": "quantitative"
        }
      },
      "mark": "rect"
    },
    {
      "data": {
        "name": "data-1acee7c5d817865a529b53e022474ce8"
      },
      "encoding": {
        "text": {
          "field": "label",
          "type": "ordinal"
        },
        "x": {
          "field": "x_min",
          "scale": {
            "zero": false
          },
          "type": "quantitative"
        },
        "y": {
          "axis": {
            "labels": false,
            "tickCount": 5,
            "ticks": false
          },
          "field": "y_med",
          "type": "quantitative"
        }
      },
      "mark": {
        "dx": -15,
        "dy": 8,
        "size": 15,
        "type": "text"
      }
    },
    {
      "data": {
        "name": "data-8e6359ea3034b8410708361bb10fafd5"
      },
      "encoding": {
        "x": {
          "axis": {
            "tickCount": 5
          },
          "field": "attempt",
          "title": "Attempt",
          "type": "quantitative"
        },
        "y": {
          "field": "score",
          "scale": {
            "domain": [
              0,
              100
            ]
          },
          "title": "Grade",
          "type": "quantitative"
        }
      },
      "mark": {
        "point": true,
        "type": "line"
      }
    }
  ]
}

答案 1 :(得分:0)

要使其正常工作,我首先必须将x和y轴的编码更改为序数。然后,在创建架构之前,我将您的输入数据值映射到字母等级

//replace every score value with correct letter grade
values.forEach(datapoint => {
  if(datapoint.score > 90) {
    datapoint.score = "A";
  } else if(datapoint.score > 80) {
    datapoint.score = "B";
  } else if (datapoint.score > 70) {
    //so on...
  }
});

这是vega-lite编辑器中的一个有效示例: Line Chart with Ordinal Values

以下是架构:

{
      "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
      "data": {
        "values": [
          {
            "attempt": 1,
            "score": "F"
          },
          {
            "attempt": 2,
            "score": "D"
          },
          {
            "attempt": 3,
            "score": "C"
          },
          {
            "attempt": 4,
            "score": "F"
          },
          {
            "attempt": 5,
            "score": "D"
          }
        ]
      },
      "mark": {
        "type": "line",
        "color": "#22bc9a",
        "point": {
          "filled": false
        }
      },
      "encoding": {
        "x": {
          "field": "attempt",
          "type": "ordinal",
          "axis": {
            "title": "Attempt"
          }
        },
        "y": {
          "field": "score",
          "type": "ordinal",
          "axis": {
            "title": "Grade"
          }
        },
        "opacity": {"value": 0.3}
      },
      "config": {
        "autosize": "fit",
        "axis": {
          "labelColor": "#bebec8",
          "tickColor": "#bebec8",
          "titleColor": "black",
          "titleFontWeight": "normal",
          "titleFontSize": 11,
          "labelFont": "Helvetica",
          "titleFont": "Helvetica",
          "gridOpacity": 0.4,
          "gridWidth": 1.5
        },
        "view": {
          "strokeWidth": 0
        }
      }
    }