formatting time axis in rbokeh

75 Views Asked by At

I am trying to create a linechart with rbokeh where the x-axis is supposed to show a datetime variable.

Consider the following example:

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

This produces the following plot:

enter image description here

Not only does this show the wrong values on the x-axis, they are also formatted in a very inconvenient way. I have tried changing the format to a more appropriate way by using the format argument (that has been commented out in my example), but this only causes the plot to not even be created anymore.

What is going on here?

1

There are 1 best solutions below

2
On BEST ANSWER

Your code worked for me with the format argument. If you want the time included, I would use this:

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)