9

数据是一系列日期和时间。

date time
2010-01-01 09:04:43
2010-01-01 10:53:59
2010-01-01 10:57:18
2010-01-01 10:59:30
2010-01-01 11:00:44
…

我的目标是用横轴 (x) 上的日期和纵轴 (y) 上的时间来表示散点图。我想如果同一日期有不止一次,我也可以添加颜色强度。

创建日期直方图非常容易。

mydata <- read.table("mydata.txt", header=TRUE, sep=" ")
mydatahist <- hist(as.Date(mydata$day), breaks = "weeks", freq=TRUE, plot=FALSE)
barplot(mydatahist$counts, border=NA, col="#ccaaaa")
  1. 我还没有弄清楚如何创建一个以日期和/或时间为轴的散点图。
  2. 我还希望能够使用线性日期 YYYY-MM-DD 而不需要轴,但也可以基于诸如 MM-DD 之类的月份(因此不同的年份会累积),甚至可以在几周内轮换。

欢迎任何帮助、RTFM URI 拍打或提示。

4

2 回答 2

16

ggplot2软件包很容易处理日期和时间。

创建一些日期和时间数据:

dates <- as.POSIXct(as.Date("2011/01/01") + sample(0:365, 100, replace=TRUE))
times <- as.POSIXct(runif(100, 0, 24*60*60), origin="2011/01/01")

df <- data.frame(
  dates = dates,
  times = times
)

Then get some ggplot2 magic. ggplot will automatically deal with dates, but to get the time axis formatted properly use scale_y_datetime():

library(ggplot2)
library(scales)
ggplot(df, aes(x=dates, y=times)) + 
  geom_point() + 
  scale_y_datetime(breaks=date_breaks("4 hour"), labels=date_format("%H:%M")) + 
  theme(axis.text.x=element_text(angle=90))

enter image description here


Regarding the last part of your question, on grouping by week, etc: To achieve this you may have to pre-summarize the data into the buckets that you want. You can use possibly use plyr for this and then pass the resulting data to ggplot.

于 2011-08-23T12:45:03.787 回答
0

I'd start by reading about as.POSIXct, strptime, strftime, and difftime. These and related functions should allow you to extract the desired subsets of your data. The formatting is a little tricky, so play with the examples in the help files.
And, once your dates are converted to a POSIX class, as.numeric() will convert them all to numeric values, hence easy to sort, plot, etc.

Edit: Andre's suggestion to play w/ ggplot to simplify your axis specifications is a good one.

于 2011-08-23T12:45:06.273 回答