gpt4 book ai didi

data-visualization - Vega-Lite:从星期一开始的一周和一般错误的周数

转载 作者:行者123 更新时间:2023-12-04 08:49:42 24 4
gpt4 key购买 nike

我是 Vega-Lite 的新手,并试图按周汇总我的数据。按周显示数据的现有选项不适合我,因为我希望本周从星期一开始(而不是现在的星期日)+实际上周数是错误的。
下面是我的基本代码。

{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {
"values": [
{"date": "2020-09-29", "count": "13", "outcome": "invalid"},
{"date": "2020-09-29", "count": "14", "outcome": "fail"},
{"date": "2020-09-29", "count": "20", "outcome": "pass"},
{"date": "2020-09-27", "count": "70", "outcome": "invalid"},
{"date": "2020-09-27", "count": "30", "outcome": "fail"},
{"date": "2020-09-27", "count": "20", "outcome": "pass"},
{"date": "2020-09-26", "count": "5", "outcome": "invalid"},
{"date": "2020-09-26", "count": "15", "outcome": "fail"},
{"date": "2020-09-26", "count": "13", "outcome": "pass"}
]
},
"width": 280,
"height": 200,
"mark": {"type": "bar", "tooltip": true},
"encoding": {
"x": {
"title": "Week",
"field": "date",
"type": "ordinal",
"timeUnit": "week",
"axis": {"format": "%W"}
},
"y": {
"title": "Number of tests",
"field": "count",
"aggregate": "sum",
"type": "quantitative",
"axis": {"orient": "right"}
},
"color": {
"field": "outcome",
"type": "nominal",
"scale": {
"domain": ["invalid", "fail", "pass"],
"range": ["#c7c7c7", "#8fd7f9", "#ef9292"]
},
"legend": {"title": "Test results"}
}
}
}
rendered plot
原则上,我可以使用下面代码片段中的窗口函数之类的东西来计算每周计数,但我有每个日期的多个实例,我不想在“结果”变量中折叠。此外,我的数据可以从任意日期开始,因此从 0 开始计算周数也不是一种选择。
{"calculate": "day(datum.date) == 0", "as": "sundays"},
{
"window": [{"op": "sum", "field": "sundays", "as": "week"}],
"sort": "date"
}

我还想到了一个不太优雅的解决方案 - 在 x 轴上采取 7 天的步骤并在 y 轴上聚合(同时确保数据从星期一开始)。这给了我每周正确的总计数,但随后我很难用周数正确标记 X 轴。
最后,即使我可以在星期天开始一周(所以使用我上面给出的基本代码),我也会看到意外的周数。出于某种原因(也许那是因为我不知道如何正确计算周数),显示的周数是 37 和 38(如附图所示),而实际上它们应该是 39 和 40。如何我要解决这个问题吗?
我会很感激任何提示。

最佳答案

Vega 的 week timeUnit 具有明确定义的行为,在 timeUnit documentation 中有详细说明。 :

"week": Sunday-based weeks. Days before the first Sunday of the year are considered to be in week 0, the first Sunday of the year is the start of week 1, the second Sunday week 2, etc..


目前没有内置到包中的替代周定义,但您可以使用 vega expressions在转换中从您的数据计算任意数量。
如果我正确完成了计算,我认为这将为您提供您所追求的 ISO 周数:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {
"values": [
{"date": "2020-09-29", "count": "14", "outcome": "fail"},
{"date": "2020-09-29", "count": "20", "outcome": "pass"},
{"date": "2020-09-27", "count": "70", "outcome": "invalid"},
{"date": "2020-09-27", "count": "30", "outcome": "fail"},
{"date": "2020-09-27", "count": "20", "outcome": "pass"},
{"date": "2020-09-26", "count": "5", "outcome": "invalid"},
{"date": "2020-09-26", "count": "15", "outcome": "fail"},
{"date": "2020-09-26", "count": "13", "outcome": "pass"}
]
},
"transform": [
{"calculate": "day(datetime(utcyear(datum.date), 0, 1))", "as": "startingDay"},
{"calculate": "(4 - datum.startingDay) % 7 - 2", "as": "mondayOfFirstWeek"},
{"calculate": "1 + floor((utcdayofyear(datum.date) - datum.mondayOfFirstWeek) / 7)", "as": "ISOweek"}
],
"width": 280,
"height": 200,
"mark": {"type": "bar", "tooltip": true},
"encoding": {
"x": {
"title": "Week",
"field": "ISOweek",
"type": "ordinal"
},
"y": {
"title": "Number of tests",
"field": "count",
"aggregate": "sum",
"type": "quantitative",
"axis": {"orient": "right"}
},
"color": {
"field": "outcome",
"type": "nominal",
"scale": {
"domain": ["invalid", "fail", "pass"],
"range": ["#c7c7c7", "#8fd7f9", "#ef9292"]
},
"legend": {"title": "Test results"}
}
}
}
enter image description here
转换的简要说明:
  • {"calculate": "day(datetime(utcyear(datum.date), 0, 1))", "as": "startingDay"},
    这将计算给定年份的 1 月 1 日是星期几(星期日=0,星期一=1...星期六=6)。
  • {"calculate": "(4 - datum.startingDay) % 7 - 2", "as": "mondayOfFirstWeek"},
    这将计算第一周开始的一年中的哪一天。因此,例如,如果 startingDay = 5 ,那么 1 月 1 日是星期五,因此一年中的第 4 天是包含星期四的第一周的星期一。如 startingDay = 4 ,则 1 月 1 日是星期四,因此第 -2 天是包含星期四的第一周的星期一。
  • {"calculate": "1 + floor((utcdayofyear(datum.date) - datum.mondayOfFirstWeek) / 7)", "as": "ISOweek"}
    这将计算从上述第一个星期一开始的 7 天周的四舍五入数。

  • 请注意,我们使用 utc解析时的 timeUnits 版本 datum.date为了正确处理不完整的时间戳,如 2020-09-29 .如果我们没有,那么 1 月 1 日的 ISOweek 将是不正确的。

    关于data-visualization - Vega-Lite:从星期一开始的一周和一般错误的周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64157070/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com