具有小时和按组的 Highcharter 时间序列不绘制
Highcharter time series with hours and by group does not plot
如何在 R 中使用 highcharter 按组(周期)制作时间序列图表?
下面的代码没有绘制线条,我不确定为什么。
a %>%
highcharter::hchart("line", hcaes(x = date, y = players, group = period)) %>%
highcharter::hc_xAxis(title = list(text = "Player"))
a <- tibble::tribble(
~date, ~players, ~period,
"2021-12-15 00:00:00", 2303, "b",
"2021-12-15 01:00:00", 2463, "b",
"2021-12-15 02:00:00", 2525, "b",
"2021-12-15 03:00:00", 2359, "b",
"2021-12-15 04:00:00", 2046, "b",
"2021-12-15 05:00:00", 1583, "b",
"2021-12-15 06:00:00", 1262, "b",
"2021-12-15 07:00:00", 879, "b",
"2021-12-15 08:00:00", 638, "b",
"2021-12-15 09:00:00", 511, "b",
"2021-12-15 10:00:00", 554, "b",
"2021-12-15 11:00:00", 788, "b",
"2021-12-15 12:00:00", 1036, "b",
"2021-12-15 13:00:00", 1300, "b",
"2021-12-15 14:00:00", 1449, "b",
"2021-12-15 15:00:00", 1727, "b",
"2021-12-15 16:00:00", 1818, "b",
"2021-12-15 17:00:00", 1916, "b",
"2021-12-15 18:00:00", 1924, "b",
"2021-12-15 19:00:00", 1918, "b",
"2021-12-15 20:00:00", 1941, "b",
"2021-12-15 21:00:00", 2042, "b",
"2021-12-15 22:00:00", 2063, "b",
"2021-12-15 23:00:00", 2160, "b",
"2021-12-16 00:00:00", 2347, "b",
"2021-12-16 01:00:00", 2348, "b",
"2021-12-16 02:00:00", 2420, "b",
"2021-12-16 03:00:00", 2295, "b",
"2021-12-16 04:00:00", 2032, "b",
"2021-12-16 05:00:00", 1582, "b",
"2021-12-16 06:00:00", 1168, "b",
"2021-12-16 07:00:00", 830, "b",
"2021-12-16 08:00:00", 637, "b",
"2021-12-16 09:00:00", 521, "b",
"2021-12-16 10:00:00", 433, "b",
"2021-12-16 11:00:00", 68, "b",
"2021-12-15 00:00:00", 2555, "a",
"2021-12-15 01:00:00", 2767, "a",
"2021-12-15 02:00:00", 2738, "a",
"2021-12-15 03:00:00", 2496, "a",
"2021-12-15 04:00:00", 2224, "a",
"2021-12-15 05:00:00", 1761, "a",
"2021-12-15 06:00:00", 1316, "a",
"2021-12-15 07:00:00", 916, "a",
"2021-12-15 08:00:00", 702, "a",
"2021-12-15 09:00:00", 551, "a",
"2021-12-15 10:00:00", 595, "a",
"2021-12-15 11:00:00", 834, "a",
"2021-12-15 12:00:00", 1156, "a",
"2021-12-15 13:00:00", 1407, "a",
"2021-12-15 14:00:00", 1627, "a",
"2021-12-15 15:00:00", 1859, "a",
"2021-12-15 16:00:00", 1974, "a",
"2021-12-15 17:00:00", 2078, "a",
"2021-12-15 18:00:00", 2129, "a",
"2021-12-15 19:00:00", 2202, "a",
"2021-12-15 20:00:00", 2177, "a",
"2021-12-15 21:00:00", 2271, "a",
"2021-12-15 22:00:00", 2367, "a",
"2021-12-15 23:00:00", 2416, "a",
"2021-12-16 00:00:00", 2606, "a",
"2021-12-16 01:00:00", 2658, "a",
"2021-12-16 02:00:00", 2747, "a",
"2021-12-16 03:00:00", 2558, "a",
"2021-12-16 04:00:00", 2256, "a",
"2021-12-16 05:00:00", 1794, "a",
"2021-12-16 06:00:00", 1269, "a",
"2021-12-16 07:00:00", 947, "a",
"2021-12-16 08:00:00", 682, "a",
"2021-12-16 09:00:00", 592, "a",
"2021-12-16 10:00:00", 623, "a",
"2021-12-16 11:00:00", 844, "a"
) %>%
dplyr::mutate(date = lubridate::ymd_hms(date))
当我像下面的代码那样转换日期变量时,它对我有用。但是,x 轴看起来很糟糕,我想将其作为日期而不是整数。
dplyr::mutate(date = highcharter::datetime_to_timestamp(date)) %>%
highcharter::hchart("line", highcharter::hcaes(x = date, y = players, group = period)) %>%
highcharter::hc_xAxis(title = list(text = "Player"))
我没有关注你的问题所在,但是当我 运行 以下代码时,我得到了以下情节。请注意,尽管调用了 highcharter
包
,我仍需要加载 ggplot2
包
# Provided data
a <- tibble::tribble(
~date, ~players, ~period,
"2021-12-15 00:00:00", 2303, "b",
"2021-12-15 01:00:00", 2463, "b",
"2021-12-15 02:00:00", 2525, "b",
"2021-12-15 03:00:00", 2359, "b",
"2021-12-15 04:00:00", 2046, "b",
"2021-12-15 05:00:00", 1583, "b",
"2021-12-15 06:00:00", 1262, "b",
"2021-12-15 07:00:00", 879, "b",
"2021-12-15 08:00:00", 638, "b",
"2021-12-15 09:00:00", 511, "b",
"2021-12-15 10:00:00", 554, "b",
"2021-12-15 11:00:00", 788, "b",
"2021-12-15 12:00:00", 1036, "b",
"2021-12-15 13:00:00", 1300, "b",
"2021-12-15 14:00:00", 1449, "b",
"2021-12-15 15:00:00", 1727, "b",
"2021-12-15 16:00:00", 1818, "b",
"2021-12-15 17:00:00", 1916, "b",
"2021-12-15 18:00:00", 1924, "b",
"2021-12-15 19:00:00", 1918, "b",
"2021-12-15 20:00:00", 1941, "b",
"2021-12-15 21:00:00", 2042, "b",
"2021-12-15 22:00:00", 2063, "b",
"2021-12-15 23:00:00", 2160, "b",
"2021-12-16 00:00:00", 2347, "b",
"2021-12-16 01:00:00", 2348, "b",
"2021-12-16 02:00:00", 2420, "b",
"2021-12-16 03:00:00", 2295, "b",
"2021-12-16 04:00:00", 2032, "b",
"2021-12-16 05:00:00", 1582, "b",
"2021-12-16 06:00:00", 1168, "b",
"2021-12-16 07:00:00", 830, "b",
"2021-12-16 08:00:00", 637, "b",
"2021-12-16 09:00:00", 521, "b",
"2021-12-16 10:00:00", 433, "b",
"2021-12-16 11:00:00", 68, "b",
"2021-12-15 00:00:00", 2555, "a",
"2021-12-15 01:00:00", 2767, "a",
"2021-12-15 02:00:00", 2738, "a",
"2021-12-15 03:00:00", 2496, "a",
"2021-12-15 04:00:00", 2224, "a",
"2021-12-15 05:00:00", 1761, "a",
"2021-12-15 06:00:00", 1316, "a",
"2021-12-15 07:00:00", 916, "a",
"2021-12-15 08:00:00", 702, "a",
"2021-12-15 09:00:00", 551, "a",
"2021-12-15 10:00:00", 595, "a",
"2021-12-15 11:00:00", 834, "a",
"2021-12-15 12:00:00", 1156, "a",
"2021-12-15 13:00:00", 1407, "a",
"2021-12-15 14:00:00", 1627, "a",
"2021-12-15 15:00:00", 1859, "a",
"2021-12-15 16:00:00", 1974, "a",
"2021-12-15 17:00:00", 2078, "a",
"2021-12-15 18:00:00", 2129, "a",
"2021-12-15 19:00:00", 2202, "a",
"2021-12-15 20:00:00", 2177, "a",
"2021-12-15 21:00:00", 2271, "a",
"2021-12-15 22:00:00", 2367, "a",
"2021-12-15 23:00:00", 2416, "a",
"2021-12-16 00:00:00", 2606, "a",
"2021-12-16 01:00:00", 2658, "a",
"2021-12-16 02:00:00", 2747, "a",
"2021-12-16 03:00:00", 2558, "a",
"2021-12-16 04:00:00", 2256, "a",
"2021-12-16 05:00:00", 1794, "a",
"2021-12-16 06:00:00", 1269, "a",
"2021-12-16 07:00:00", 947, "a",
"2021-12-16 08:00:00", 682, "a",
"2021-12-16 09:00:00", 592, "a",
"2021-12-16 10:00:00", 623, "a",
"2021-12-16 11:00:00", 844, "a"
)
运行代码:
library(ggplot2)
a %>%
highcharter::hchart("line", highcharter::hcaes(x = date, y = players, group = period)) %>%
highcharter::hc_xAxis(title = list(text = "Players"))
returns 此图在 RStudio 的“查看器”选项卡(而非“图”)中。
如何在 R 中使用 highcharter 按组(周期)制作时间序列图表?
下面的代码没有绘制线条,我不确定为什么。
a %>%
highcharter::hchart("line", hcaes(x = date, y = players, group = period)) %>%
highcharter::hc_xAxis(title = list(text = "Player"))
a <- tibble::tribble(
~date, ~players, ~period,
"2021-12-15 00:00:00", 2303, "b",
"2021-12-15 01:00:00", 2463, "b",
"2021-12-15 02:00:00", 2525, "b",
"2021-12-15 03:00:00", 2359, "b",
"2021-12-15 04:00:00", 2046, "b",
"2021-12-15 05:00:00", 1583, "b",
"2021-12-15 06:00:00", 1262, "b",
"2021-12-15 07:00:00", 879, "b",
"2021-12-15 08:00:00", 638, "b",
"2021-12-15 09:00:00", 511, "b",
"2021-12-15 10:00:00", 554, "b",
"2021-12-15 11:00:00", 788, "b",
"2021-12-15 12:00:00", 1036, "b",
"2021-12-15 13:00:00", 1300, "b",
"2021-12-15 14:00:00", 1449, "b",
"2021-12-15 15:00:00", 1727, "b",
"2021-12-15 16:00:00", 1818, "b",
"2021-12-15 17:00:00", 1916, "b",
"2021-12-15 18:00:00", 1924, "b",
"2021-12-15 19:00:00", 1918, "b",
"2021-12-15 20:00:00", 1941, "b",
"2021-12-15 21:00:00", 2042, "b",
"2021-12-15 22:00:00", 2063, "b",
"2021-12-15 23:00:00", 2160, "b",
"2021-12-16 00:00:00", 2347, "b",
"2021-12-16 01:00:00", 2348, "b",
"2021-12-16 02:00:00", 2420, "b",
"2021-12-16 03:00:00", 2295, "b",
"2021-12-16 04:00:00", 2032, "b",
"2021-12-16 05:00:00", 1582, "b",
"2021-12-16 06:00:00", 1168, "b",
"2021-12-16 07:00:00", 830, "b",
"2021-12-16 08:00:00", 637, "b",
"2021-12-16 09:00:00", 521, "b",
"2021-12-16 10:00:00", 433, "b",
"2021-12-16 11:00:00", 68, "b",
"2021-12-15 00:00:00", 2555, "a",
"2021-12-15 01:00:00", 2767, "a",
"2021-12-15 02:00:00", 2738, "a",
"2021-12-15 03:00:00", 2496, "a",
"2021-12-15 04:00:00", 2224, "a",
"2021-12-15 05:00:00", 1761, "a",
"2021-12-15 06:00:00", 1316, "a",
"2021-12-15 07:00:00", 916, "a",
"2021-12-15 08:00:00", 702, "a",
"2021-12-15 09:00:00", 551, "a",
"2021-12-15 10:00:00", 595, "a",
"2021-12-15 11:00:00", 834, "a",
"2021-12-15 12:00:00", 1156, "a",
"2021-12-15 13:00:00", 1407, "a",
"2021-12-15 14:00:00", 1627, "a",
"2021-12-15 15:00:00", 1859, "a",
"2021-12-15 16:00:00", 1974, "a",
"2021-12-15 17:00:00", 2078, "a",
"2021-12-15 18:00:00", 2129, "a",
"2021-12-15 19:00:00", 2202, "a",
"2021-12-15 20:00:00", 2177, "a",
"2021-12-15 21:00:00", 2271, "a",
"2021-12-15 22:00:00", 2367, "a",
"2021-12-15 23:00:00", 2416, "a",
"2021-12-16 00:00:00", 2606, "a",
"2021-12-16 01:00:00", 2658, "a",
"2021-12-16 02:00:00", 2747, "a",
"2021-12-16 03:00:00", 2558, "a",
"2021-12-16 04:00:00", 2256, "a",
"2021-12-16 05:00:00", 1794, "a",
"2021-12-16 06:00:00", 1269, "a",
"2021-12-16 07:00:00", 947, "a",
"2021-12-16 08:00:00", 682, "a",
"2021-12-16 09:00:00", 592, "a",
"2021-12-16 10:00:00", 623, "a",
"2021-12-16 11:00:00", 844, "a"
) %>%
dplyr::mutate(date = lubridate::ymd_hms(date))
当我像下面的代码那样转换日期变量时,它对我有用。但是,x 轴看起来很糟糕,我想将其作为日期而不是整数。
dplyr::mutate(date = highcharter::datetime_to_timestamp(date)) %>%
highcharter::hchart("line", highcharter::hcaes(x = date, y = players, group = period)) %>%
highcharter::hc_xAxis(title = list(text = "Player"))
我没有关注你的问题所在,但是当我 运行 以下代码时,我得到了以下情节。请注意,尽管调用了 highcharter
包
ggplot2
包
# Provided data
a <- tibble::tribble(
~date, ~players, ~period,
"2021-12-15 00:00:00", 2303, "b",
"2021-12-15 01:00:00", 2463, "b",
"2021-12-15 02:00:00", 2525, "b",
"2021-12-15 03:00:00", 2359, "b",
"2021-12-15 04:00:00", 2046, "b",
"2021-12-15 05:00:00", 1583, "b",
"2021-12-15 06:00:00", 1262, "b",
"2021-12-15 07:00:00", 879, "b",
"2021-12-15 08:00:00", 638, "b",
"2021-12-15 09:00:00", 511, "b",
"2021-12-15 10:00:00", 554, "b",
"2021-12-15 11:00:00", 788, "b",
"2021-12-15 12:00:00", 1036, "b",
"2021-12-15 13:00:00", 1300, "b",
"2021-12-15 14:00:00", 1449, "b",
"2021-12-15 15:00:00", 1727, "b",
"2021-12-15 16:00:00", 1818, "b",
"2021-12-15 17:00:00", 1916, "b",
"2021-12-15 18:00:00", 1924, "b",
"2021-12-15 19:00:00", 1918, "b",
"2021-12-15 20:00:00", 1941, "b",
"2021-12-15 21:00:00", 2042, "b",
"2021-12-15 22:00:00", 2063, "b",
"2021-12-15 23:00:00", 2160, "b",
"2021-12-16 00:00:00", 2347, "b",
"2021-12-16 01:00:00", 2348, "b",
"2021-12-16 02:00:00", 2420, "b",
"2021-12-16 03:00:00", 2295, "b",
"2021-12-16 04:00:00", 2032, "b",
"2021-12-16 05:00:00", 1582, "b",
"2021-12-16 06:00:00", 1168, "b",
"2021-12-16 07:00:00", 830, "b",
"2021-12-16 08:00:00", 637, "b",
"2021-12-16 09:00:00", 521, "b",
"2021-12-16 10:00:00", 433, "b",
"2021-12-16 11:00:00", 68, "b",
"2021-12-15 00:00:00", 2555, "a",
"2021-12-15 01:00:00", 2767, "a",
"2021-12-15 02:00:00", 2738, "a",
"2021-12-15 03:00:00", 2496, "a",
"2021-12-15 04:00:00", 2224, "a",
"2021-12-15 05:00:00", 1761, "a",
"2021-12-15 06:00:00", 1316, "a",
"2021-12-15 07:00:00", 916, "a",
"2021-12-15 08:00:00", 702, "a",
"2021-12-15 09:00:00", 551, "a",
"2021-12-15 10:00:00", 595, "a",
"2021-12-15 11:00:00", 834, "a",
"2021-12-15 12:00:00", 1156, "a",
"2021-12-15 13:00:00", 1407, "a",
"2021-12-15 14:00:00", 1627, "a",
"2021-12-15 15:00:00", 1859, "a",
"2021-12-15 16:00:00", 1974, "a",
"2021-12-15 17:00:00", 2078, "a",
"2021-12-15 18:00:00", 2129, "a",
"2021-12-15 19:00:00", 2202, "a",
"2021-12-15 20:00:00", 2177, "a",
"2021-12-15 21:00:00", 2271, "a",
"2021-12-15 22:00:00", 2367, "a",
"2021-12-15 23:00:00", 2416, "a",
"2021-12-16 00:00:00", 2606, "a",
"2021-12-16 01:00:00", 2658, "a",
"2021-12-16 02:00:00", 2747, "a",
"2021-12-16 03:00:00", 2558, "a",
"2021-12-16 04:00:00", 2256, "a",
"2021-12-16 05:00:00", 1794, "a",
"2021-12-16 06:00:00", 1269, "a",
"2021-12-16 07:00:00", 947, "a",
"2021-12-16 08:00:00", 682, "a",
"2021-12-16 09:00:00", 592, "a",
"2021-12-16 10:00:00", 623, "a",
"2021-12-16 11:00:00", 844, "a"
)
运行代码:
library(ggplot2)
a %>%
highcharter::hchart("line", highcharter::hcaes(x = date, y = players, group = period)) %>%
highcharter::hc_xAxis(title = list(text = "Players"))
returns 此图在 RStudio 的“查看器”选项卡(而非“图”)中。