Plot durations as lines with ggplot2

Viewed 3199

Last days, I've pondering the following problem:I want to plot the duration of an activity as a line, with the y axis representing the date the activity happens and the x axis representing time in hours. When I deal with an activity that starts and ends in the same day this is easy.

For example:

df1<-structure(list(Date = structure(c(16802, 16803, 16805, 16806, 
16809, 16810, 16812, 16813, 16816, 16820, 16821, 16822, 16829
), class = "Date"), hms1 = structure(c(1457623680, 1457620860, 
1457621160, 1457622540, 1457625600, 1457621280, 1457620380, 1457619720, 
1457623620, 1457621460, 1457620440, 1457617980, 1457621880), class =      c("POSIXct", "POSIXt"), tzone = ""), hms2 = structure(c(1457632500, 1457627640, 
1457628360, 1457629500, 1457631000, 1457625120, 1457625420, 1457624520, 
1457627640, 1457626800, 1457626800, 1457622060, 1457625540), class = c("POSIXct", 
"POSIXt"), tzone = "")), .Names = c("Date", "hms1", "hms2"), class =     "data.frame", row.names = c(103L, 105L, 108L, 110L, 114L, 117L, 120L, 122L, 127L, 135L, 136L, 138L, 145L))

p1<-ggplot(df1, aes(x=Date,y= hms1))+ scale_x_date(breaks = date_breaks("1 day"))+
geom_linerange(aes(ymin = hms1, ymax = hms2),color = "red",size = 2)+ coord_flip()
p1+ylab("Time")+ggtitle("Activity During Day")

gives desired plot: ggplot1

Note that the real date, is on column Date while the dates in hms1m hms2 is wrong due to the process that I used to get the time in H:M:S format.

df1$hms1 <- format(df1$time, format = "%H:%M:%S")
df1$hms1 <- as.POSIXct(df1$hms1, format = "%H:%M:%S")

Thing is that most of the "activities", I'm trying to plot span in two day periods. The only solution I have come up with up to now, is shifting both the datetime values by an appropriate period, in order to "artificialy" place them in the same day, as suggested here 2 . This process though, naturally results in the x axis displaying the "artificial" timestamps.

How could I take control of the tick marks in ggplot, so I can make them display the right timestamps? Or even better can someone propose a more elegant solution? To be clear: I want the y-axis to be just the date of the start of the event, but the x-axis to span more than 24 hours.

1 Answers
Related