4

What is the smartest way to manipulate POSIX for use in ggplot axis?

I am trying to create a function for plotting many graphs (One per day) spanning a period of weeks, using POSIX time for the x axis.

To do so, I create an additional integer column DF$Day with the day, that I input into the function. Then, I create a subset using that day, which I plot using ggplot2. I figured how to use scale_x_datetime to format the POSIX x axis. Basically, I have it show the hours & minutes only, omitting the date.

Here is my question: How can I set the limits for each individual graph in hours of the day?

Below is some working, reproducible code to get an idea. It creates the first day, shows it for 3 seconds & the proceeds to create the second day. But, each days limits is chosen based on the range of the time variable. How can I make the range, for instance, all day long (0h - 24h)?

DF <- data.frame(matrix(ncol = 0, nrow = 4))

DF$time <- as.POSIXct(c("2010-01-01 02:01:00", "2010-01-01 18:10:00", "2010-01-02 04:20:00", "2010-01-02 13:30:00"))
DF$observation <- c(1,2,1,2)
DF$Day <- c(1,1,2,2)

for (Individual_Day in 1:2) { 
Day_subset <- DF[DF$Day == as.integer(Individual_Day),]

print(ggplot( data=Day_subset, aes_string( x="time", y="observation") ) + geom_point() +
      scale_x_datetime( breaks=("2 hour"), minor_breaks=("1 hour"), labels=date_format("%H:%M")))

Sys.sleep(3) }

Second plot created by the function, to show the axis

rcs
  • 67,191
  • 22
  • 172
  • 153
M. Momtaz Hegazy
  • 173
  • 1
  • 12
  • Rather than printing graphs and using `Sys.sleep()`, you might want to check out the `animation` package. The [`saveHTML`](http://www.inside-r.org/packages/cran/animation/docs/saveHTML) function comes with a nice "player" that lets you step through frame by frame or adjust a playback speed in a browser. You can also save a GIF or other format. – Gregor Thomas Jan 22 '14 at 20:08
  • @shujaa I'll be sure to check that. I actually googled `sys.sleep()` for this question, normally I just save to PDF. Assumed that would make it easier for posting; Thanks for the advice :) – M. Momtaz Hegazy Jan 22 '14 at 20:20

1 Answers1

3

Well, here's one way.

# ...
for (Individual_Day in 1:2) { 
Day_subset <- DF[DF$Day == as.integer(Individual_Day),]
lower <- with(Day_subset,as.POSIXct(strftime(min(time),"%Y-%m-%d")))
upper <- with(Day_subset,as.POSIXct(strftime(as.Date(max(time))+1,"%Y-%m-%d"))-1)
limits = c(lower,upper)

print(ggplot( data=Day_subset, aes( x=time, y=observation) ) + 
        geom_point() +
        scale_x_datetime( breaks=("2 hour"), 
                          minor_breaks=("1 hour"), 
                          labels=date_format("%H:%M"),
                          limits=limits)
      )
}

The calculation for lower takes the minimum time in the subset and coerces it to character with only the date part (e.g., strips away the time part). Converting back to POSIXct generates the beginning of that day.

The calculation for upper is a little more complicated. You have to convert the maximum time to a Date value and add 1 (e.g., 1 day), then convert to character (strip off the time part), convert back to POSIXct, and subtract 1 (e.g., 1 second). This generates 23:59 on the end day.

Huge amount of work for such a small thing. I hope someone else posts a simpler way to do this...

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • That worked alright. I am starting to doubt using POSIX for plotting though, it is too complicated. I relied on an additional Vector at first, where I made integers out of the hours/minutes & just plotted on that numeric vector. I tried POSIX hoping it would be more elegant, but the opposite seems true... – M. Momtaz Hegazy Jan 22 '14 at 20:23
  • @Hegazy You might want to un-accept this answer. I'm interested too, and as jlhoward says "I hope someone else posts a simpler way to do this...". Give it a couple days, and then if no one has posted a simpler answer, accept this one. But if it's marked as accepted, the question won't get much attention. – Gregor Thomas Jan 22 '14 at 20:30
  • Agreed. The convention on SO is to wait at least 24 hours before accepting an answer (assuming of course that it solves your problem). See [this link] (http://stackoverflow.com/help/someone-answers) for some guidelines. – jlhoward Jan 22 '14 at 21:28
  • update: dplyr 1.0.0 throws an error `could not find function "date_format"` – Agile Bean May 19 '21 at 02:55