0

我正在尝试创建一个折线图,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参数(在我的示例中已注释掉)将格式更改为更合适的方式,但这只会导致不再创建绘图。

这里发生了什么?

4

1 回答 1

1

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

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)
于 2019-08-29T13:39:54.287 回答