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

时间:2018-04-25 20:41:31

标签: r plotly r-plotly

我有一个plotly的情节,在x轴上有'时间'(例如5:00 AM),但有些标签包括前导0(例如05:00 AM)。我想删除这些领先的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")
  )

enter image description here

如何摆脱那些讨厌的领先0?

1 个答案:

答案 0 :(得分:1)

您应该使用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")
  )

enter image description here