我正在制作一个分层图表,其中既有条形图又有规则线。我遇到的问题是,在 x 轴上,刻度线和刻度标签仅出现在条形下方,不会跨越整个轴,因此在规则线下方没有刻度线和标签位于。这是我所看到的示例( link to Vega editor ):
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {"url": "data/movies.json"},
"transform": [
{"calculate": "2*datum.IMDB_Rating", "as": "UpperLimit"}
],
"layer": [
{
"mark": "bar",
"encoding": {
"x": {"bin": true, "field": "IMDB_Rating", "type": "quantitative"},
"y": {"aggregate": "count", "type": "quantitative"}
}
},
{
"mark": "rule",
"encoding": {
"x": {
"aggregate": "max",
"field": "UpperLimit",
"type": "quantitative"
},
"color": {"value": "red"},
"size": {"value": 5}
}
}
]
}
Image of issue
如何让刻度线和标签跨越整个轴?先谢谢您的帮助!
最佳答案
当您在编码中使用 bin 转换时,Vega-Lite 会调整默认轴属性以匹配 binning。您可以通过编码的 scale
和 axis
属性手动重新调整这些,但我认为更简单的方法是将 bin 转换移动到图表的 transform
规范。这是一个示例( Vega Editor ):
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {"url": "data/movies.json"},
"transform": [
{"calculate": "2*datum.IMDB_Rating", "as": "UpperLimit"},
{
"bin": true,
"field": "IMDB_Rating",
"as": ["IMDB_Rating_0", "IMDB_Rating_1"]
}
],
"layer": [
{
"mark": "bar",
"encoding": {
"x": {
"field": "IMDB_Rating_0",
"type": "quantitative",
"bin": "binned"
},
"x2": {"field": "IMDB_Rating_1"},
"y": {"aggregate": "count", "type": "quantitative"}
}
},
{
"mark": "rule",
"encoding": {
"x": {
"aggregate": "max",
"field": "UpperLimit",
"type": "quantitative"
},
"color": {"value": "red"},
"size": {"value": 5}
}
}
]
}
关于Vega-Lite 分层图表 : how to get tick marks and tick labels to span the entire axis?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59685475/