r - Histogram over time of day -
i have dataframe in 1 column contains different dates including times, e.g.
as.posixct(c("2017-07-03 08:23:00", "2017-07-03 09:00:00", "2017-07-03 17:23:00", "2017-07-03 18:05:00", "2017-07-04 08:24:00", "2017-07-04 09:02:00", "2017-07-04 17:24:00", "2017-07-04 18:01:00", "2017-07-05 08:57:00", "2017-07-05 09:31:00", "2017-07-05 16:25:00", "2017-07-05 17:14:00"))
now want @ how many times time occurs @ intervals (say 15 min). thus, aim histogram of frequency (over days) vs time of day.
any hints?
edit: tried extract time by
df$time <- hm(format(df$date, "%h:%m"))
but left me column of class period didn't know how handle. tried like
ggplot(df, aes(date)) + geom_histogram() + scale_x_time()
my main problem here how can use ggplot plotting.
probably easier way this, approach...
library(plyr) library(lubridate) #sample data df<-data.frame(time=as.posixct(c( '2017-07-03 08:23:00', '2017-07-03 09:00:00', '2017-07-03 17:23:00', '2017-07-03 18:05:00', '2017-07-04 08:24:00', '2017-07-04 09:02:00', '2017-07-04 17:24:00', '2017-07-04 18:01:00', '2017-07-05 08:57:00', '2017-07-05 09:31:00', '2017-07-05 16:25:00', '2017-07-05 17:14:00'))) #extract time df$hour = hour(df$time) + minute(df$time)/60 + second(df$time)/3600 #create bins bins=c(paste0(rep(c(paste0(0,0:9),10:23), each=4),".", c("00",25,50,75))[-1],"24:00") #divide data bins df$bins = cut(df$hour, breaks=seq(0, 24, 0.25), labels=bins) #reformat numeric df$bins <- as.numeric(as.character(df$bins)) #histogram hist(df$bins) #with ggplot library(ggplot2) ggplot(df, aes(bins)) + geom_histogram()
Comments
Post a Comment