在 hh:mm:ss 中显示 Plotly 直方图时间

Displaying Plotly histogram time in hh:mm:ss

我有一个马拉松完成时间的向量,我想使用 x-axis/bins 以小时和分钟 hh:mm:ss 格式使用 R 中的 plot_ly 函数绘制为直方图。

library(plotly)

R<-c("02:17:20", "02:17:26", "02:19:17", "02:19:18", "02:20:50", "02:21:20")

plot_ly(x = as.difftime(R), type = "histogram")

图表上的 x 轴和弹出窗口以十进制格式显示,即 2.325 而不是 2:19:30 我想这是因为我使用的是 difftimeclass.是否可以更改图表的格式以显示为 hh:mm:ss 格式?

Plot_ly running times histogram

您可以使用 相同的技巧,即将您的小时数转换为日期时间。假设它们都在 1970/01/01 的某个时间完成,但我们只会报告小时数 (tickformat="%H:%M:%S")。

请注意,这看起来与您原来的示例有点不同,因为 plotly 任意决定以不同方式放置 bin。

library(plotly)
library('magrittr')

R<-c("02:17:20", "02:17:26", "02:17:46", "02:18:26", "02:18:46", "02:19:17", "02:19:18", "02:20:50", "02:21:20")

R <- paste('1970-01-01', R)
R <- strptime(R, format="%Y-%m-%d %H:%M:%S")

plot_ly(x = (as.numeric(R) * 1000), type = "histogram") %>% 
  layout(xaxis=list(type="date", tickformat="%H:%M:%S"))