问题
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 difftime
class. 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