我正在使用Vega-Lite在包含马来西亚选举数据的数据集上创建一些可视化,特别是在第14届选举和第15届选举中每个政党在每个州赢得的席位的数据集。这里是link to the dataset。
我想实现一个彗星图,显示两次选举之间的变化,类似于the one exhibited in Vega-Lite's example gallery。
我修改了我的选举数据集,以匹配示例中使用的barley dataset的结构。为清楚起见,y轴应为沿着州,x轴应为沿着选举,x轴应为每个政党沿着一列。所以election
取代year
,state
取代variety
,party
取代site
,seats
取代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编码,从不同的变量旋转和折叠,调整比例
我所期望的是:一张和维加画廊里的彗星图相似的彗星图
实际结果是:如上所述,具有垂直轨迹和缺失水平轨迹的图表
1条答案
按热度按时间mwngjboj1#
请试试这个。