使用ggplot2绘制时间序列图

时间:2012-05-31 20:27:16

标签: r ggplot2

  

可能重复:
  Plot dates on the x axis and time on the y axis with ggplot2

我有一张包含时间,日期和价值的表格。例如,

time    date        value
00:01   2012-04-01  100
00:02   2012-04-01  150
00:03   2012-04-01  130
...
00:01   2012-04-02  80
00:02   2012-04-02  160
00:03   2012-04-02  20 
...
03:01   2012-04-06  120
03:02   2012-04-06  160
04:03   2012-04-06  70  
...

我需要绘制时间(x轴),值(y轴)的图表。我正在使用ggplot2和lubridate,但是不能沿着x轴每隔30分钟制作一个R图。大约有3000行(多个日期从00:00到23:59,几分钟几次)。

有关如何实现这一目标的任何建议。我尝试过使用因子(时间),但是R并没有把它当作时间值(所以,它会尝试标记从2012-04-01 00:01到2012-04-06 23:59的连续值

1 个答案:

答案 0 :(得分:0)

感谢所有反馈。

scale_datetime的参数在0.9中已更改。因此,正如所建议的那样,我使用标签而不是格式,并使用date_breaks函数设置中断。

为了创建图表,我添加了一个日期时间字段 -

df$comb <- ymd_hm(paste(df$date, df$time))

定义了函数,

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

qplot(dropDate(df$comb), df$average, ylim=c(0,20), geom="smooth", colour=df$date) + scale_x_datetime(breaks = date_breaks("1 hours"), labels=date_format("%H")) + xlab("Hour in UTC") + ylab("Average")
相关问题