r Plotly 无法让 xaxis tickmode="array" 为日期工作

r Plotly can't get xaxis tickmode="array" working for dates

我正在尝试让 xaxis tickmode="array" 为绘图的日期工作。

这是一个例子:

x <- as.Date(c("2016-08-12", "2016-08-13", "2016-08-14", 
               "2016-08-15", "2016-08-16"))
y <- c(1, 2, 3, 4, 5)
df <- data.frame(x,y)

str(df)

plot_ly(df, x = x, y = y) %>%
layout(xaxis = list(
  tickmode = "array",
  tickvals =
    c(as.numeric(as.POSIXct("2016-08-12", format="%Y-%m-%d"))*1000,
      as.numeric(as.POSIXct("2016-08-16", format="%Y-%m-%d"))*1000),
  type = "date"))

我发现各种帖子都说 plotly 只能理解毫秒形式的日期(谁知道?)。如果我用 range= 代替 tickvals=,上面的代码就可以工作。我还没有找到任何方法来影响使用 tickmode = "array" 显示的日期。建议非常感谢。

欢迎使用 Whosebug!

我不清楚预期的结果,但我猜你正在搜索 ticktext

有关详细信息,请参阅 schema()

编辑:我提交了一个问题here

但是,这里有一个解决方法 - 请检查以下内容:

library(plotly)

df <- data.frame(x = c("2016-08-12", "2016-08-13", "2016-08-14", 
                       "2016-08-15", "2016-08-16"), index = 1:5, y = 1:5)

plot_ly(df, x = ~index, y = ~y, type = "scatter", mode = "markers") %>%
  layout(xaxis = list(
    tickmode = "array",
    tickvals = ~index,
    ticktext = ~x))