0

How can I format the x axis so it can display in hh:mm:ss?

This is a sample of my set:

structure(list(Catraca = c(1L, 253L, 72L, 49L, 2L, 391L, 53L, 
52L, 505L, 206L, 105L, 169L, 180L, 222L, 165L, 192L, 286L, 129L, 
337L, 270L), Horario = structure(c(360L, 660L, 1740L, 1800L, 
1980L, 1980L, 2280L, 2280L, 6060L, 6660L, 6720L, 7200L, 7260L, 
7920L, 8460L, 8460L, 8580L, 8580L, 9000L, 9060L), class = "ITime")), .Names = c("Catraca", 
"Horario"), class = c("data.table", "data.frame"), row.names = c(NA, 
-20L))

The first column are integers, and the second are ITime class, that came from a POSIXct attribute, because I just want to display the hh:mm:ss only.

And this is how I tried to plot: qplot(data = b, x = Horario, y = Catraca ). I know that I need to format the Horario some way, but I don't find it on the web.I need your help.

Note: R studio displays this message Don't know how to automatically pick scale for object of type ITime. Defaulting to continuous.

Homunculus
  • 329
  • 2
  • 12

2 Answers2

1

ITime is a class specific to the data.table package, so qplot() doesn't know how to handle it (and just acts on the underlying integer values).

I don't think base R has a time-only class, so you'll probably need to use a package. The chron package would work, as here, although ggplot2 might play more nicely with the hms package since it's part of the tidyverse.

EDIT based on answer from Nova.

b$time <- as.POSIXct(paste0("2017-09-28 ", b$Horario))
qplot(data = b, x = time, y = Catraca)

However, unless the total amount of lapsed time is really small, I doubt it will ever show seconds since it picks axis ticks/labels based on larger rounded units of time (e.g., hours, half hours).

If you really want to customize how the axis ticks and labels appear, it'd be easiest to switch from qplot() to ggplot().

treysp
  • 663
  • 6
  • 17
  • What I am more concern is minutes, so no problem. The plot looks much better. Now I have to find out how to get rid off the *Set 28* that came with it haha – Homunculus Sep 28 '17 at 18:49
1

Can you post a better example of your data? I'm getting error messages when I try to make it a data.frame.

For this kind of problem, a simple work around is to make the time and date a POSIXct type, but give it all the same date. Usually the plotting program will then only display the times on the axis and it will ignore the date.

Nova
  • 5,423
  • 2
  • 42
  • 62
  • The problem with the data is now solved, sorry for that. The set is now usable (you need to import data.table library). Changing the topic, the suggestion you gave me is nice, lets see how qplot displays – Homunculus Sep 28 '17 at 18:31