使用ggplot2在x轴上绘制日期和y轴上的时间 [英] Plot dates on the x axis and time on the y axis with ggplot2

查看:368
本文介绍了使用ggplot2在x轴上绘制日期和y轴上的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一系列37个日期和时间,发生了一件事。它现在坐在一个POSIXlt对象。我想要每天发生事件的时代的图形表示。所以x轴应该是日期,y轴应该是一天中的时间。



然后我试图用ggplot2绘制

  qplot(day(dttm),hour(dttm))

这是我想要的,但它没有分钟的分辨率。



以下是一些示例数据

  dttm 
[1]2011-11-16 10:39:202011-11-16 10:56:322011-11-16 11:52:43 2011-11-16 12:10:42
[5]2011-11-16 13:10:132011-11-16 13:41:102011-11-16 13:48:072011-11-16 14:54:04
[9]2011-11-17 07:05:232011-11-17 07:34:24 2011-11-17 07:53:012011-11-17 07:57:04
[13]2011-11-17 08:09:162011-11-17 08: 23:432011-11-17 10:20:542011-11-17 10:45:13
[17]2011-11-17 10:49:322011- 11-17 11:16:082011-11-17 11:24:052011-11-17 11:50:11
[21]2011-11-17 11:52: 2011-11-17 11:54:422011-11-17 11:55:252011-11-17 11:57:34
[25]2011-11- 17 12:06:152011-11-17 12:08:052011-11-17 12:08:332011-11-17 12:30:13
[29] 2011-11-17 13:24:412011-11-17 13:44:412011-11-17 13:48:552011-11-17 14:59:08
[33]2011-11-18 06:46:172011-11-18 07:52:502011-11-18 08:31:222011-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,6L,8L,8L,30L,24L,
44L,16L,24L,50L,52L,54L, ,48L,59L,46L,52L,31L,33L,50L),小时= c(10L,10L,11L,
12L,13L,13L,13L,14L,7L,7L,7L,7L,8L, 8L,10L,10L,10L,11L,11L,11L,11L,11L,11L,12L,12L,12L,12L,13L,13L,
13L,14L,6L,7L, 8L,8L,8L,17L,17L,17L,17L,17L,17L,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,1 0L,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,111B,111L,111L,111L,111L,111L,111L,111L, $ b),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,320L,320L,320L,320L,320L,320L,320B,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)),.Names = c(sec,min,
hour,mday,mon,year,wday,yday,isdst),class = c(POSIXlt ,
POSIXt))


解决方案

需要两个步骤:




  • 从POSIXct对象中提取时间元素。您可以使用一些 lubridate 提取器函数和一些算术,或者减去 as.Date(dttm)来自 dttm 。我显示两种方式。

  • 添加日期时间y轴并指定合适的格式化程序



从POSIXct对象中提取时间的两种替代方法:

  dropDate<  -  function(x){
3600 *小时(x)+ 60 *分钟(x)+秒(x)
}

dropDate2< - function(x){
as.numeric - as.Date(x))
}

您还可以指定显式标签对于轴:

  qplot(day(dttm),dropDate(dttm))+ 
scale_y_datetime(format = %H:%M:%S)+
xlab(Day)+ ylab(Hour)



< hr>

?scale_datetime 中有更多这种类型的缩放示例,这将指向 ?strptime ,以了解日期和时间格式代码的说明。


I have read in a series of 37 dates and times that an event happened. It is now sitting as a POSIXlt object. I want a graphic representation of the times that the events happened on each day. So the x axis should be the date and y axis should be the time of day.

then I tried to plot it with ggplot2

qplot(day(dttm), hour(dttm))

That is kind of what I want but it does not have the resolution of minutes. How do I have hours and minutes included in the y axis?

Here is some sample data

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"

Here is the dput file

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"))

解决方案

There are two steps required:

  • Extract the time element from the POSIXct object. You can do that with some lubridate extractor functions and a bit of arithmetic, or by subtracting as.Date(dttm) from dttm. I show both ways.
  • Add a date-time y-axis and specify a suitable formatter

Two alternative ways to extract the time from a POSIXct object:

dropDate <- function(x){
  3600*hour(x)+60*minute(x)+second(x)
}

dropDate2 <- function(x){
  as.numeric(x - as.Date(x))
}  

You may also wish to specify explicit labels for the axes:

qplot(day(dttm), dropDate(dttm)) + 
    scale_y_datetime(format="%H:%M:%S") + 
    xlab("Day") + ylab("Hour")


There are more examples of this type of scale in ?scale_datetime, which will point you to ?strptime for an explanation of the date and time formatting codes.

这篇关于使用ggplot2在x轴上绘制日期和y轴上的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆