简体   繁体   中英

Highcharter time series with hours and by group does not plot

How can I make a time series chart by group (period) with highcharter in R?

The code below does not plot the lines and I am not sure why.

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))

It works for me when I transform the date variable like in the code below. However, the x-axis looks bad and I would like to have it as a date instead of an integer.

  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"))

I'm not following where your issue is, but when I run the following code, I get the following plot. Note that I needed to load the ggplot2 package despite calling the highcharter package

# 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"
) 

Run code:

library(ggplot2)

a %>%
  highcharter::hchart("line", highcharter::hcaes(x = date, y = players, group = period)) %>%
  highcharter::hc_xAxis(title = list(text = "Players"))

Which returns this plot in the "Viewer" tab (not "Plots") of RStudio. 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM