我会将时间向量放入 data.frame 的列中。下面的示例数据有一千个时间戳,时间随机,介于当前时间和两年后。
dat = data.frame(timestamp = Sys.time() + sort(round(runif(1000, (24*3600), (2*365*24*3600)))))
下一步是创建一个新列,用于标识时间戳所在的月份和年份:
dat$month = strftime(dat$time, "%b")
dat$year = strftime(dat$time, "%Y")
现在我们可以使用plyr 包中的count 计算每年每月的时间戳。
library(plyr)
timestamps_month = count(dat, vars = c("month","year"))
并使用 ggplot2 创建直方图:
library(ggplot2)
ggplot(data = timestamps_month) + geom_bar(aes(x = month, y = freq, fill = year), stat="identity", position = "dodge")
请参阅此 SO 帖子以获取结果图的示例:
How to create histogram in R with CSV time data?