Plotly: как убрать ведущие нули из меток тиков - PullRequest
0 голосов
/ 25 апреля 2018

У меня есть график с «временем суток» на оси 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

Как я могу получитьизбавиться от этих надоедливых ведущих 0s?

1 Ответ

0 голосов
/ 28 апреля 2018

Вы должны использовать tickformat = "%-I:%M %p" (подробнее см. здесь )

# 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

...