Vega-lite: Add week Time Unit

Created on 17 Apr 2020  路  13Comments  路  Source: vega/vega-lite

Hi,
it could be very useful to be able to aggregate by week, or to have a generic step definition aggregation.

Thank you

Most helpful comment

Looks like its just missing from the docs and schema. I'll add it.

All 13 comments

I think that needs to be done in Vega similar to https://github.com/vega/vega/pull/2530.

Vega already has a week time unit. See https://vega.github.io/vega/docs/transforms/timeunit/

Thank you @domoritz and @jheer .

I have asked in mailing list, they told me that there is no way to do it in vega-lite and to open here a feature request.

Can I use Vega time unit to aggregate in example in Vega-lite this by week? How to do it?

Looks like its just missing from the docs and schema. I'll add it.

Was there a reason we didn't add week in https://github.com/vega/vega-lite/pull/5577 @haldenl @arvind?

I have a great reply from Jake VanderPlas. I have edited it, I still have to understand it well (I'm still a beginner); I have to understand for example why day(datum.date) == 2 is monday, but it seems to me a good solution

Was there a reason we didn't add week in #5577 @haldenl @arvind?

I think it's simply because we didn't notice that Vega's new timeunit support week, which Vega-Lite's old implementation did not.

temporal type

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Using `labelExpr` to show only initial letters of month names.",
  "data": {"url": "data/seattle-weather.csv"},
  "mark": "bar",
  "encoding": {
    "x": {
      "timeUnit": "week",
      "field": "date",
      "type": "temporal"
    },
    "y": {
      "aggregate": "mean",
      "field": "precipitation",
      "type": "quantitative"
    }
  }
}

image

  • the tick doesn't really start at the first one.
  • The last week is somehow 00.

Ordinal type

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Using `labelExpr` to show only initial letters of month names.",
  "data": {"url": "data/seattle-weather.csv"},
  "mark": "bar",
  "encoding": {
    "x": {
      "timeUnit": "week",
      "field": "date",
      "type": "ordinal"
    },
    "y": {
      "aggregate": "mean",
      "field": "precipitation",
      "type": "quantitative"
    }
  }
}

image

  • There are two W52 (one at the beginning)

Since this is broken, I'm reverting them in https://github.com/vega/vega-lite/pull/6376. We can revert the PR later to re-enable them.

The first week is 52. That's odd and the spec looks right. Maybe this has to do with the fact that the week number is defined such that the first week of the year is the week that contains that year's first Thursday.

Screen Shot 2020-04-20 at 16 16 47

Spec to play with

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {
    "values": [
      {"date": "2020/01/01"},
      {"date": "2020/01/02"},
      {"date": "2020/01/03"},
      {"date": "2020/01/04"},
      {"date": "2020/01/05"},
      {"date": "2020/01/06"},
      {"date": "2020/01/07"},
      {"date": "2020/01/08"},
      {"date": "2020/01/09"},
      {"date": "2020/01/10"},
      {"date": "2020/01/11"}
    ],
    "format": {"parse": {"date": "date"}}
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "date", "aggregate": "count", "type": "quantitative"},
    "y": {"field": "date", "timeUnit": "yearday", "type": "ordinal"}
  }
}

Will be fixed in https://github.com/vega/vega-lite/pull/6526, pending Vega fixes.

Was this page helpful?
0 / 5 - 0 ratings