【问题标题】:Plotly: How to remove leading 0s from tick labelsPlotly:如何从刻度标签中删除前导 0
【发布时间】:2018-04-25 20:41:31
【问题描述】:

我有一个plotly 在 x 轴上带有“时间”的图(例如上午 5:00),但一些标签包含前导 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 = "%H %p"
  • @MLavoie 没有。该格式仍然有前导 0,并且将“2PM”错误地显示为“14PM”。

标签: r plotly r-plotly


【解决方案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")
  )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-05
    • 2021-12-11
    • 2020-04-24
    • 2020-03-30
    • 1970-01-01
    • 2021-10-13
    • 2022-10-20
    • 2018-07-19
    相关资源
    最近更新 更多