json Vega-Lite:彗星图轨迹沿沿着两个轴行进,而它应该只沿水平方向行进

unftdfkk  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在使用Vega-Lite在包含马来西亚选举数据的数据集上创建一些可视化,特别是在第14届选举和第15届选举中每个政党在每个州赢得的席位的数据集。这里是link to the dataset
我想实现一个彗星图,显示两次选举之间的变化,类似于the one exhibited in Vega-Lite's example gallery
我修改了我的选举数据集,以匹配示例中使用的barley dataset的结构。为清楚起见,y轴应为沿着州,x轴应为沿着选举,x轴应为每个政党沿着一列。所以election取代yearstate取代varietyparty取代siteseats取代yield
在对模板代码进行了相关更改之后,我得到以下内容

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "https://raw.githubusercontent.com/botsunny/FIT3179-Viz-2/main/data/wrangled_ge14_15.csv"},
  "title": "Change in seats by state among major coalitions from GE14 to GE15",
  "transform": [
    {"pivot": "election", "value": "seats", "groupby": ["state", "party"]},
    {"fold": ["14", "15"], "as": ["election", "seats"]},
    {"calculate": "toNumber(datum.election)", "as": "election"},
    {"calculate": "datum['15'] - datum['14']", "as": "delta"}
  ],
  "mark": "trail",
  "encoding": {
    "x": {"field": "election", "title": null},
    "y": {"field": "state", "title": "state"},
    "size": {
      "field": "seats",
      "type": "quantitative",
      "scale": {"range": [0, 15]},
      "legend": {"values": [5, 15]},
      "title": "Seats won"
    },
    "tooltip": [{"field": "election", "type": "quantitative"}, {"field": "seats"}],
    "color": {
      "field": "delta",
      "type": "quantitative",
      "scale": {"domainMid": 0},
      "title": "Change in seats"
    },
    "column": {"field": "party", "title": "Coalation"}
  },
  "view": {"stroke": null},
  "config": {"legend": {"orient": "bottom", "direction": "horizontal"}}
}

然而,由此产生的可视化看起来像这样(目前没有足够的声誉来发布图像):https://i.stack.imgur.com/fnDhl.png
这是我在Vega编辑器中的代码。
彗星的轨迹似乎是垂直的,也是水平的。我是相当新的维加建兴,并试图发挥周围调试这个问题无济于事。感谢您的帮助!
我尝试的是:交换x和y编码,从不同的变量旋转和折叠,调整比例
我所期望的是:一张和维加画廊里的彗星图相似的彗星图
实际结果是:如上所述,具有垂直轨迹和缺失水平轨迹的图表

mwngjboj

mwngjboj1#

请试试这个。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "url": "https://raw.githubusercontent.com/botsunny/FIT3179-Viz-2/main/data/wrangled_ge14_15.csv"
  },
  "title": "Change in seats by state among major coalitions from GE14 to GE15",
  "transform": [
    {"pivot": "election", "value": "seats", "groupby": ["state", "party"]},
    {"fold": ["14", "15"], "as": ["election", "seats"]},
    {"calculate": "toNumber(datum.election)", "as": "election"},
    {"calculate": "datum['15'] - datum['14']", "as": "delta"}
  ],
  "spacing": 2,
  "facet": {
    "row": {
      "field": "state",
      "type": "nominal",
      "title": null,
      "header": {"labelAngle": 0, "labelAlign": "left"}
    },
    "column": {"field": "party", "type": "nominal", "title": "Coalition"}
  },
  "spec": {
    "width": 150,
    "mark": "trail",
    "encoding": {
      "x": {"field": "election", "title": null, "type": "ordinal"},
      "size": {
        "field": "seats",
        "type": "quantitative",
        "scale": {"range": [1, 25]},
        "legend": {"values": [0, 20]},
        "title": "Seats won"
      },
      "tooltip": [
        {"field": "election", "type": "quantitative"},
        {"field": "seats"}
      ],
      "color": {
        "field": "delta",
        "type": "quantitative",
        "scale": {"domainMid": 0},
        "title": "Change in seats"
      }
    },
    "view": {"stroke": null}
  },
  "config": {"legend": {"orient": "bottom", "direction": "horizontal"}}
}

相关问题