我已经阅读了一系列事件发生的37个日期和时间。它现在作为POSIXlt对象。我想要一个图形表示事件发生在每一天的时间。所以x轴应该是日期,y轴应该是一天中的时间。
然后我尝试用ggplot2
绘制它qplot(day(dttm), hour(dttm))
这就是我想要的东西,但它没有分钟的分辨率。如何在y轴中包含小时和分钟?
以下是一些示例数据
dttm
[1] "2011-11-16 10:39:20" "2011-11-16 10:56:32" "2011-11-16 11:52:43" "2011-11-16 12:10:42"
[5] "2011-11-16 13:10:13" "2011-11-16 13:41:10" "2011-11-16 13:48:07" "2011-11-16 14:54:04"
[9] "2011-11-17 07:05:23" "2011-11-17 07:34:24" "2011-11-17 07:53:01" "2011-11-17 07:57:04"
[13] "2011-11-17 08:09:16" "2011-11-17 08:23:43" "2011-11-17 10:20:54" "2011-11-17 10:45:13"
[17] "2011-11-17 10:49:32" "2011-11-17 11:16:08" "2011-11-17 11:24:05" "2011-11-17 11:50:11"
[21] "2011-11-17 11:52:47" "2011-11-17 11:54:42" "2011-11-17 11:55:25" "2011-11-17 11:57:34"
[25] "2011-11-17 12:06:15" "2011-11-17 12:08:05" "2011-11-17 12:08:33" "2011-11-17 12:30:13"
[29] "2011-11-17 13:24:41" "2011-11-17 13:44:41" "2011-11-17 13:48:55" "2011-11-17 14:59:08"
[33] "2011-11-18 06:46:17" "2011-11-18 07:52:50" "2011-11-18 08:31:22" "2011-11-18 08:33:43"
[37] "2011-11-18 08:50:08"
这是dput文件
structure(list(sec = c(20, 32, 43, 42, 13, 10, 7, 4, 23, 24,
1, 4, 16, 43, 54, 13, 32, 8, 5, 11, 47, 42, 25, 34, 15, 5, 33,
13, 41, 41, 55, 8, 17, 50, 22, 43, 8), min = c(39L, 56L, 52L,
10L, 10L, 41L, 48L, 54L, 5L, 34L, 53L, 57L, 9L, 23L, 20L, 45L,
49L, 16L, 24L, 50L, 52L, 54L, 55L, 57L, 6L, 8L, 8L, 30L, 24L,
44L, 48L, 59L, 46L, 52L, 31L, 33L, 50L), hour = c(10L, 10L, 11L,
12L, 13L, 13L, 13L, 14L, 7L, 7L, 7L, 7L, 8L, 8L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L,
13L, 14L, 6L, 7L, 8L, 8L, 8L), mday = c(16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 18L, 18L, 18L, 18L, 18L), mon = c(10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L), year = c(111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L
), wday = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), yday = c(319L, 319L, 319L, 319L,
319L, 319L, 319L, 319L, 320L, 320L, 320L, 320L, 320L, 320L, 320L,
320L, 320L, 320L, 320L, 320L, 320L, 320L, 320L, 320L, 320L, 320L,
320L, 320L, 320L, 320L, 320L, 320L, 321L, 321L, 321L, 321L, 321L
), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec", "min",
"hour", "mday", "mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt",
"POSIXt"))
答案 0 :(得分:9)
需要两个步骤:
lubridate
提取器函数和一些算术,或从as.Date(dttm)
中减去dttm
。我表现出两种方式。从POSIXct对象中提取时间的两种替代方法:
dropDate <- function(x){
3600*hour(x)+60*minute(x)+second(x)
}
dropDate2 <- function(x){
as.numeric(x - as.Date(x))
}
您可能还希望为轴指定显式标签:
qplot(day(dttm), dropDate(dttm)) +
scale_y_datetime(format="%H:%M:%S") +
xlab("Day") + ylab("Hour")
?scale_datetime
中有更多此类比例的示例,它会指向?strptime
以解释日期和时间格式代码。
答案 1 :(得分:3)
您可以使用scale_datetime调整日期轴。最后的例子非常具有说明性。