1

Possible Duplicate:
Plot dates on the x axis and time on the y axis with ggplot2

I have a table with times, date and a value. For eg.,

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  
...

I need to plot a graph of time (x-axis), value (y-axis). I am using ggplot2 and lubridate, but can't make R plot in intervals of say every 30 mins along the x-axis. There are about 3000 rows (several times in minutes from 00:00 to 23:59 for multiple dates).

Any suggestions on how this can be accomplished. I have tried using factor(time), but R doesn't treat it as a time value (so, instead it tries to label continuous values from 00:01 of 2012-04-01 to 23:59 of 2012-04-06.

Community
  • 1
  • 1
xbsd
  • 2,438
  • 4
  • 25
  • 35
  • 2
    Very nearly a duplicate of [this](http://stackoverflow.com/q/8192343/324364). – joran May 31 '12 at 20:45
  • 1
    And, if this is allowed, a pointer to my own answer on [Understanding dates and plotting a histogram with ggplot2 in R](http://stackoverflow.com/a/10776953/1290420). I illustrate two ways of using `scale_x_date()` and `scale_x_datetime()` -- modifying the axis `label`s – daedalus Jun 01 '12 at 08:07

1 Answers1

0

Thanks for all the feedback.

The parameters for scale_datetime has changed in 0.9. So, as suggested, I used labels instead of format and set the breaks using date_breaks function.

In order to create the graph, I added a datetime field --

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

Defined the function,

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")
xbsd
  • 2,438
  • 4
  • 25
  • 35