I have a dataset with a column in string of format hh:mm:ss. I want to create a histogram based on this column in such a way that I can visualize the number of observations between 12 AM and 3 PM in R.
plot_ly(x = (as.numeric(data$Time) * 1000), type = "histogram") %>%
layout(xaxis=list(type="date", tickformat="%H:%M:%S"))
I tried plotting using Plotly but the x-axis is in a different format than expected. Please give suggestions.
One approach could be the use of the hms library
library("hms")
As there was no data provided I generated some random data for an easier understanding. The as_hms() function transforms the values as a difftime vector with a custom class
Count <- c(10,20,30,100,110,110,20,30,50,30)
Time <- c('12:02:01','12:07:38','12:30:42','12:57:21','13:01:09','13:38:36','13:48:43','13:51:33','14:50:22','14:59:59')
Time = as_hms(c(Time))
data = data.frame(Count, Time)
With ggplot you can now easily create an histogram with the number of observations. And if you need explicitly a plotly visualization you can achieve this with the library ggplotly.
p <- ggplot(data=data, aes(x=Time, y=Count)) +
geom_bar(stat="identity")
ggplotly(p)