以 hh:mm:ss 显示 Plotly 直方图时间 [英] Displaying Plotly histogram time in hh:mm:ss

查看:54
本文介绍了以 hh:mm:ss 显示 Plotly 直方图时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

图书馆(情节)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 我认为这是因为我使用的是difftime 类.是否可以更改图表的格式以将其显示为 hh:mm:ss 格式?

I have a vector of marathon finishing times I want to plot as a histogram with the x-axis/bins in hours and minutes hh:mm:ss format using the plot_ly function in R.

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

The x-axis and the popup on the chart displays in decimal format i.e 2.325 instead of 2:19:30 I assume this is because I'm using the difftimeclass. Is it possible to change the formatting of the graph to show it in hh:mm:ss format?

Plot_ly running times histogram

解决方案

You could do the same trick like here, i.e. convert your hours into datetime. Let's pretend they all finished on 1970/01/01 at some time, but we will only report the hours (tickformat="%H:%M:%S").

Please note that this will look a bit different than your original example, because plotly arbitrarily decides to place the bins differently.

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

这篇关于以 hh:mm:ss 显示 Plotly 直方图时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆