Histogram over time of day

2019-08-27 14:15发布

问题:

I have a dataframe in which one 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 I want to look at how many times a certain time occurs at intervals (say 15 min). Thus, I aim to get a histogram of frequency (over all days) vs time of day.

Any hints?

Edit: I tried to extract the time by

df$Time <- hm(format(df$Date, "%H:%M"))

but this left me with a column of class period that I didn't know how to handle. I also tried something like

ggplot(df, aes(Date)) +
geom_histogram() +
scale_x_time()

My main problem here is how can I use ggplot to do the plotting.

回答1:

Probably an easier way to do this, but this is my 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 Into Bins
df$bins = cut(df$hour, breaks=seq(0, 24, 0.25), labels=bins)

#Reformat to Numeric
df$bins <- as.numeric(as.character(df$bins))

#Histogram
hist(df$bins)

#With ggplot
library(ggplot2)
ggplot(df, aes(bins)) +
  geom_histogram()


标签: r ggplot2