一天中的直方图 [英] Histogram over time of day
本文介绍了一天中的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,其中一列包含不同的日期,包括时间,例如
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"))
现在,我想看看某个时间间隔(例如15分钟)发生了多少次.因此,我的目标是获得频率(整天)与一天中时间的直方图.
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.
有任何提示吗?
我尝试通过以下方式提取时间
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()
我的主要问题是如何使用ggplot进行绘图.
My main problem here is how can I use ggplot to do the plotting.
推荐答案
可能是更简单的方法,但这是我的方法...
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()
这篇关于一天中的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文