Displaying Plotly histogram time in hh:mm:ss

心已入冬 提交于 2020-12-16 07:42:30

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/42413282/displaying-plotly-histogram-time-in-hhmmss

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!