PAYATHON 2026

Deneb 如何按年份分开同一城市的点?

数据阿明

结论

两层的 x 编码目前都使用 City,因此所有 City 3 记录会落在同一个 x 轴类别位置。sort 只调整类别顺序,不能按日期或年份分开同一类别中的点。要让点按年份分布,需要把两层的 x 字段都设为数值型 Year,并用 City 区分折线。

修改配置

将折线层和点层的 x 编码都设为 Year,并为两层添加相同的 City 颜色编码:

{
  "width": 570,
  "data": {"name": "dataset"},
  "layer": [
    {
      "mark": {"type": "line", "tooltip": true},
      "encoding": {
        "x": {
          "field": "Year",
          "type": "quantitative",
          "axis": {"title": "Year"}
        },
        "y": {
          "field": "Level",
          "type": "quantitative",
          "axis": {"title": "Level"}
        },
        "color": {"field": "City", "type": "nominal"}
      }
    },
    {
      "mark": {"type": "point", "tooltip": true, "filled": true},
      "encoding": {
        "x": {
          "field": "Year",
          "type": "quantitative",
          "axis": {"title": "Year"}
        },
        "y": {
          "field": "Level",
          "type": "quantitative",
          "axis": {"title": "Level"}
        },
        "color": {"field": "City", "type": "nominal"}
      }
    }
  ]
}

这样,City 3 的记录会分别显示在 2020 到 2025 对应的年份位置,颜色则按城市区分。由于 Year 是整数,应使用 quantitative 类型。确认 Deneb 视觉对象的数据字段中包含 Year。

按具体日期定位

如果需要按实际日期间隔放置点,可以把两层的 x 编码都改为:

"x": {
  "field": "Date",
  "type": "temporal",
  "axis": {"title": "Date"}
}

保留两层的 City 颜色编码,并确认 Deneb 数据中包含 Date。按年份展示时使用 Year;需要体现日期间隔时使用 Date。

注意事项

改用 Year 或 Date 后,横轴会显示时间,而不是 City 类别。line 层和 point 层应使用相同的 x、y 字段,以确保点和线的位置一致。示例数据中的 YearId 是字符串;如果使用它,应将其作为离散轴处理。按年份排列的数值轴应优先使用整数型 Year。

备注:内容仅供参考。