几个月来,我记下每天早上醒来的时间。我现在拥有的是一个包含24小时格式的时间的数据库,例如2014-11-29 05:23:00
,我可以修剪为04:23
。
我想描绘一下我醒来时间的分布情况。 x轴是一天中的时间,y轴是频率。一切都很简单,除了:
我现在正在解决的问题是如何处理x轴刻度。由于有60分钟到一个小时,我可以:
创建一天中的分钟刻度,其中04:23的时间将转换为分钟263.这在我的计算中很容易,但是读起来不直观。当然,我可以轻松地改变这些时间。
使用一百分钟。由于我的绘图中的轴只会每隔一小时标记一次,因此这既易于计算又易于阅读。但是如果我想在60分钟内看到平均值或其他计算数据,我必须重新转换它,这可能会导致不准确。但我想这些都是次要的。
让R处理时间。
由于我唯一不知道该怎么办的是第三种选择,我的问题是:
如何在R中使用时间作为数据?这样做的最佳方式是什么?
如果你想尝试一下,这是一个时间的矢量样本:
t <- c("00:13:00", "00:30:00", "00:36:00", "00:45:00", "00:48:00", "01:08:00", "01:14:00", "01:15:00", "01:25:00", "02:06:00", "02:07:00", "02:22:00", "02:23:00", "02:36:00", "02:37:00", "02:55:00", "03:08:00", "03:10:00", "03:11:00", "03:13:00", "03:15:00", "03:23:00", "03:35:00", "03:55:00", "03:57:00", "03:58:00", "04:03:00", "04:06:00", "04:15:00", "04:21:00", "04:21:00", "04:22:00", "04:43:00", "04:48:00", "04:51:00", "04:58:00", "05:00:00", "05:02:00", "05:03:00", "05:17:00", "05:25:00", "05:34:00", "05:38:00", "05:45:00", "05:46:00", "05:50:00", "05:52:00", "06:10:00", "06:11:00", "06:13:00", "06:23:00", "06:26:00", "22:18:00", "23:27:00", "23:40:00", "23:53:00", "23:54:00", "23:58:00")
我尝试使用chron
库绘制时间,但由于某种原因,当范围为整个24小时时,x轴的标记将恢复为0到1(它显示图表的时间)只有几个小时宽,hist
函数拒绝使用任何图形参数(plot
仍为FALSE
,即使我明确将其设置为TRUE
:
library(chron)
t <- times(c("00:13:00", "00:30:00", "00:36:00", "00:45:00", "00:48:00", "01:08:00", "01:14:00", "01:15:00", "01:25:00", "02:06:00", "02:07:00", "02:22:00", "02:23:00", "02:36:00", "02:37:00", "02:55:00", "03:08:00", "03:10:00", "03:11:00", "03:13:00", "03:15:00", "03:23:00", "03:35:00", "03:55:00", "03:57:00", "03:58:00", "04:03:00", "04:06:00", "04:15:00", "04:21:00", "04:21:00", "04:22:00", "04:43:00", "04:48:00", "04:51:00", "04:58:00", "05:00:00", "05:02:00", "05:03:00", "05:17:00", "05:25:00", "05:34:00", "05:38:00", "05:45:00", "05:46:00", "05:50:00", "05:52:00", "06:10:00", "06:11:00", "06:13:00", "06:23:00", "06:26:00", "22:18:00", "23:27:00", "23:40:00", "23:53:00", "23:54:00", "23:58:00"))
hist(t, probability = TRUE, col = "gray")
lines(density(t), col = "blue", lwd = 2)
lines(density(t, adjust = 2), lty = "dotted", col = "darkgreen", lwd = 2)
Warning message:
In hist.default(t, probability = TRUE, col = "gray", plot = FALSE) :
arguments ‘freq’, ‘col’ are not made use of
答案 0 :(得分:3)
library(ggplot2)
#generate random times (between 4AM and 7:59AM) as a proxy for your data
Random_times=c();
for(i in 1:600){
Random_times=c(Random_times,as.POSIXct(strptime(paste(sample(4:7,1),":",sample(0:59,1),":","00",sep=""),"%H:%M")))
}
#as absolute times
P_random_times=as.POSIXct(Random_times, origin="1970-01-01")
qplot(P_random_times)+xlim(c(strptime("03:00","%H:%M"),strptime("10:00","%H:%M")))
#Or as mins from the minumum wake time
P_times=difftime(P_random_times, min(P_random_times),units="mins")
qplot(as.numeric(P_times))
答案 1 :(得分:0)
答案 2 :(得分:0)
你有没有考虑使用一些任意的&#34;零&#34;点?它可能是一些最小值或平均唤醒时间。我可以想象你感兴趣的是时间之间的差异,所以&#34;零&#34;可以是任意时间点作为比较的锚点。