Plotly:如何从刻度标签中删除前导 0

Plotly: How to remove leading 0s from tick labels

我有一个 plotly 图,x 轴上有 'time of day'(例如 5:00 AM),但一些标签包括前导 0(例如 05:00是)。我想删除这些前导 0,但不确定如何删除。

示例:

library(plotly)

# Data
start <- as.POSIXct("2018-01-01 00:00:00", tz = "America/Chicago")
end <- as.POSIXct("2018-01-02 00:00:00", tz = "America/Chicago")
times <- seq.POSIXt(start, end, by = "60 mins")
df <- data.frame(x = times, y = seq_along(times))

# Plot
plot_ly() %>%
  add_trace(
    data = df,
    x = ~x,
    y = ~y,
    type = "scatter",
    mode = "lines+markers",  # "lines",
    line = list(shape = "linear", dash = "dot", width = 3),
    marker = list(size = 12)
  ) %>%
  layout(
    title = "Foo",
    xaxis = list(title = NA, showgrid = TRUE, autotick = F, dtick = 1000*60*60*2, tickformat = "%I:%M %p")
  )

我怎样才能去掉那些讨厌的前导 0?

您应该使用 tickformat = "%-I:%M %p"(有关详细信息,请参阅 here

# Plot
plot_ly() %>%
  add_trace(
    data = df,
    x = ~x,
    y = ~y,
    type = "scatter",
    mode = "lines+markers",  # "lines",
    line = list(shape = "linear", dash = "dot", width = 3),
    marker = list(size = 12)
  ) %>%
  layout(
    title = "Foo",
    xaxis = list(title = NA, showgrid = TRUE, autotick = F, dtick = 1000*60*60*2, tickformat = "%-I:%M %p")
  )