3
votes

what is the correct way to plot time series data where each row has a hh:mm:ss time stamp using ggplot2? for example:

48:09:35  0.2
48:09:36  0.3
48:12:05  0.4
48:35:48  0.5

each time stamp above is of the form: hours:minutes:seconds, like 48:09:35.

i'd like to plot this on the x-axis and the value associated with the time stamp on y axis, but have the x-axis spaced appropriately for hour-minute-second time stamps.

is there a way to do this in ggplot2 in R?

1

1 Answers

2
votes

As your times are out of daily ranges of 24 hours one cannot use a standard function (e.g. chron from the chron package) you could convert the Time variable to a numeric value.

df$Time_secs <- unlist(lapply(lapply(strsplit(df$Time, ":"), as.numeric), function(x) x[1]*60^2+x[2]*60+x[3]))
ggplot(df, aes(x = Time_secs, y = Value)) + geom_line() + scale_x_continuous(labels = df$Time)

enter image description here

Data:

dput(df)
structure(list(Time = c("48:09:35", "48:09:36", "48:12:05", "48:35:48"
), Value = c(0.2, 0.3, 0.4, 0.5)), .Names = c("Time", "Value"
), class = "data.frame", row.names = c(NA, -4L))