Search code examples
vega-lite

Ordering of months in xaxis of Vega Lite plot


I have this code which uses year information in the dates but having the time unit as 'month' seems to force it to month ordering. This goes away if i switch to 'yearmonth', but is there a way to still only show the months but have the ordering based on the year information?


Solution

  • The month timeUnit strips away all year information by design. If you want to keep the year information in the axis, you can use the yearmonth timeunit, and use an appropriate axis format string to hide the year (Vega-Lite's temporal format strings come from d3-date-format).

    Your x encoding might look something like this:

    "x": {
      "field": "date",
      "type": "ordinal",
      "timeUnit": "yearmonth",
      "axis": {"title": null, "format": "%b"}
    },