我每小时都有数量的数据。我想按小时汇总数量并绘制结果。我附上了我用过的代码。我得到的结果是一个空白的图,x轴上没有日期。我从互联网上获取了一些样本数据并将其保存为csv文件进行练习。部分数据如下:
date quantity
11/02/2011 14:30 123.53
11/02/2011 15:00 123.78
11/02/2011 15:30 124.24
11/02/2011 16:00 124.2
11/02/2011 16:30 124.07
11/02/2011 17:00 123.91
11/02/2011 17:30 123.44
11/02/2011 18:00 123.0616
11/02/2011 18:30 123.06
11/02/2011 19:00 123.13
11/02/2011 19:30 123.745
11/02/2011 20:00 123.96
11/02/2011 20:30 123.99
11/02/2011 21:00 123.99
11/03/2011 14:30 124.3
11/03/2011 15:00 124.38
11/03/2011 15:30 124.67
11/03/2011 16:00 125.19
11/03/2011 16:30 124.9
11/03/2011 17:00 125.27
11/03/2011 17:30 125.5
11/03/2011 18:00 125.58
11/03/2011 18:30 125.91
11/03/2011 19:00 125.8
11/03/2011 19:30 125.83
11/03/2011 20:00 126.215
11/03/2011 20:30 126.25
我使用的代码是:
groupdf<- read.csv
groupdf$date <- as.POSIXct(strptime(groupdf$date, format = "%d/%m/%Y %H:%M", tz = "GMT"))
groupdf$quantity <- as.numeric(levels (groupdf$quantity)) [groupdf$quantity]
groupdfres <- aggregate(groupdf$quantity,
by= list (format(groupdf$date, "%Y-%m-%d %H")), sum)
names (groupdfres) <- c ("date", "count")
groupdfres
plot (groupdfres$date, groupdfres$count, type = "l", xlab = "Time", ylab = "Count",
xlim = as.POSIXct(c("2011-01-11 00:00:00", "2011-10-11 23:59:00")))