【问题标题】:Displaying Plotly histogram time in hh:mm:ss以 hh:mm:ss 显示 Plotly 直方图时间
【发布时间】:2017-02-23 10:36:55
【问题描述】:

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

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

【问题讨论】:

    标签: r plotly


    【解决方案1】:

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

    请注意,这看起来与您的原始示例有点不同,因为情节任意决定以不同的方式放置垃圾箱。

    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"))
    

    【讨论】:

    • 非常感谢。只是为了我在plot_ly 调用中的理解,您将数字日期时间乘以 1000,因为在 plotly/js 中,日期时间从 1970/01/01 开始计算为毫秒,在 R 中计算为秒?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多