traggatmot
traggatmot

Reputation: 1463

Plot HH:MM:SS formatted data in R, when HH goes above 24

I have a data set (tt) that consists of values like these:

"76:12:49" "74:11:32" "62:39:26" "57:17:17" "43:10:59" "29:06:00" "21:32:30" "16:00:36" "13:31:27" " 9:55:12" " 9:30:34"

I can't seem to come up with a way of plotting these values. Zoo/chron combo doesn't work because times(tt) can't process anything with an HH above 23.

I'm assuming ggplot's time plotting capabilities won't work here either, since strptime similarly won't work on anything with HH above 23.

Any ideas?

Upvotes: 0

Views: 190

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269346

Read the character times into a data.frame, hms, with columns h, m and s and then compute the number and fraction of days for each. Finally plot your data vs. days:

# test data in character form
ch <- c("76:12:49", "74:11:32", "62:39:26", "57:17:17", "43:10:59", "29:06:00", "21:32:30", "16:00:36", "13:31:27", "9:55:12", "9:30:34")

hms <- read.table(text = ch, sep = ":", col.names = c("h", "m", "s"))
days <- with(hms, h / 24 + m / (24 * 60) + s / (24 * 60 * 60))
plot(seq_along(days) ~ days, ylab = "")

If hours are preferred then:

hours <- 24 * days
plot(seq_along(hours) ~ hours, ylab = "")

To get nicer day/times create a custom axis:

library(chron)

plot(seq_along(days) ~ days, ylab = "", xaxt = "n")
at <- axTicks(1)
axis(1, at = at, paste(at %/% 1, times(at %% 1)), cex.axis = .8)

enter image description here

Upvotes: 2

Related Questions