在vega-lite中订购标称轴值

时间:2017-06-08 12:41:36

标签: vega-lite

我有一个vega-lite分层条形图来比较prert和postrt的刻度调查结果。 x轴具有相似的选项,但这些选项按字母顺序排序。可以订购x轴值的顺序吗?例如"非常不同意,不同意,神经,同意,非常同意"?

示例vega-lite代码:

{
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"description": "A bar chart showing the US population distribution of     age groups and gender in 2000.",
"data": { 
  "values": [
    {"type":"pre", "answer":"Strongly Disagree", "total":15},
    {"type":"post", "answer":"Strongly Disagree", "total":30},
    {"type":"pre", "answer":"Disagree", "total":15},
    {"type":"post", "answer":"Disagree", "total":30},
    {"type":"pre", "answer":"Neutral", "total":15},
    {"type":"post", "answer":"Neutral", "total":30},
    {"type":"pre", "answer":"Neutral", "total":15},
    {"type":"post", "answer":"Neutral", "total":30},
    {"type":"pre", "answer":"Agree", "total":15},
    {"type":"post", "answer":"Agree", "total":30},
    {"type":"pre", "answer":"Strongly Agree", "total":20},
    {"type":"post", "answer":"Strongly Agree", "total":40}
  ]
},
"mark": "bar",
"encoding": {
    "x": {
      "field": "answer", "type": "nominal"
    },
    "y": {
      "field": "total", "type": "quantitative",
      "axis": {"title": "% Counts"},
      "stack": "none"
    },
    "color": {
      "field": "type", "type": "nominal",
      "axis": {"title": "Answer"},
      "scale": {"range": ["#e377c2","#1f77b4"]}
    },
    "opacity": {"value": 0.7}
}
}

1 个答案:

答案 0 :(得分:2)

您可以将x.scale.domain用于此目的。其他sort方法描述为here

以下是使用vega 3.0.0-beta.34vega-lite 2.0.0-beta.4生效的规范:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "description": "A bar chart showing the US population distribution of age groups and gender in 2000.",
  "data": {
    "values": [{
      "type": "pre",
      "answer": "Strongly Disagree",
      "total": 15
    }, {
      "type": "post",
      "answer": "Strongly Disagree",
      "total": 30
    }, {
      "type": "pre",
      "answer": "Disagree",
      "total": 15
    }, {
      "type": "post",
      "answer": "Disagree",
      "total": 30
    }, {
      "type": "pre",
      "answer": "Neutral",
      "total": 15
    }, {
      "type": "post",
      "answer": "Neutral",
      "total": 30
    }, {
      "type": "pre",
      "answer": "Neutral",
      "total": 15
    }, {
      "type": "post",
      "answer": "Neutral",
      "total": 30
    }, {
      "type": "pre",
      "answer": "Agree",
      "total": 15
    }, {
      "type": "post",
      "answer": "Agree",
      "total": 30
    }, {
      "type": "pre",
      "answer": "Strongly Agree",
      "total": 20
    }, {
      "type": "post",
      "answer": "Strongly Agree",
      "total": 40
    }]
  },
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "answer",
      "type": "nominal",
      "scale": {
        "domain": [
          "Strongly Disagree", 
          "Disagree", 
          "Neutral", 
          "Agree", 
          "Strongly Agree"
        ]
      },
      "axis": { "title": "Answer" }
    },
    "y": {
      "field": "total",
      "type": "quantitative",
      "axis": {
        "title": "% Counts"
      },
      "stack": "none"
    },
    "color": {
      "field": "type",
      "type": "nominal",
      "scale": {
        "range": ["#e377c2", "#1f77b4"]
      }
    },
    "opacity": {
      "value": 0.7
    }
  }
}