CCZ23
CCZ23

Reputation: 151

How to plot time from seconds to HH/MM

I have data such that

TIME<- c(36655,14330,23344,9992,...)

which represents seconds after midnight. I want to produce a histogram using ggplot2 so that i can see the distribution of the data BUT including the time during the day on the x-axis rather than seconds.

SO far I have:

ggplot(data=df1, aes(TIME)) + geom_histogram(col="red", fill="green", alpha = .2,bins=9)+ 
labs(title="Histogram for call time") +labs(x="Time", y="Count")

But this just gives the seconds. However, if i then convert it by:

TIME <- as.POSIXct(strptime(TIME, format="%R"))

this includes today's date which i do NOT want. I just want times i.e. split into c(0:00, 9:00, 12:00, 18:00, 24:00). Is this possible?

Upvotes: 1

Views: 421

Answers (1)

CCZ23
CCZ23

Reputation: 151

As per comments, this works:

TIME <- as.POSIXct(strptime(TIME, format="%R"))

ggplot(data=df1, aes(TIME)) + geom_histogram(col="red", fill="green", 
alpha = .2,bins=9)+ scale_x_datetime(date_labels = "%R")

Upvotes: 1

Related Questions