在 rbokeh 中格式化时间轴

formatting time axis in rbokeh

我正在尝试使用 rbokeh 创建折线图,其中 x 轴应该显示日期时间变量。

考虑以下示例:

data <- tibble(
  time = as.POSIXct(c("2019-08-27 08:15:00",
           "2019-08-27 10:30:00",
           "2019-08-27 12:45:00")),
  value = c(0.3, 0.6, 0.2)
)

figure(data = data) %>%
  ly_lines(x = time,
           y = value) %>%
  ly_points(x = time,
            y = value,
            hover = value) %>%
  x_axis(label = "Date"
         # number_formatter = "numeral", 
         # format = list(hours = "%d %B %Y")
  ) %>%
  y_axis(label = "Values", 
         number_formatter = "numeral",
         format = list(percent = "0 %")) %>% 
  y_range(dat = c(0, 1))

这会产生以下情节:

这不仅会在 x 轴上显示错误的值,而且它们的格式也非常不方便。 我尝试使用 format 参数(在我的示例中已将其注释掉)将格式更改为更合适的方式,但这只会导致甚至不再创建绘图。

这是怎么回事?

你的代码使用格式参数对我有用。如果你想要包括时间,我会用这个:

figure(data = data) %>%
  ly_lines(x = time,
           y = value) %>%
  ly_points(x = time,
            y = value,
            hover = value) %>%
  x_axis(label = "Date",
          number_formatter = "numeral", 
          format = list(hours = "%Y-%m-%d %H:%M:%S")
  ) %>%
  y_axis(label = "Values", 
         number_formatter = "numeral",
         format = list(percent = "0 %")) %>% 
  y_range(dat = c(0, 1)) %>%
  theme_axis("x", major_label_orientation = 45)