0

I have time in the format of HH:MM:SS. An example is something like this.

data.frame(time=c('08:00:00', '08:10:00'), Y=1:2)

I want to plot it in the x-axis and the corresponding numerical value in the y-axis.

The time can be plotted like the following.

https://community.rstudio.com/t/unable-to-plot-a-scatter-plot-plotting-character-against-numeric-values/42958/2

plot(Y ~ I(1:nrow(d)), data = d, axes = FALSE, xlab = "time", type = "b")
axis(side = 2)
axis(side = 1, at = 1:nrow(d), labels = d$time)

But when the time is not uniformly spaced, the plot is still uniformly spaced, which is not ideal.

Hourly time series plotting

The above treats the data as a time series. But I don't need the date part (year-month-day).

What is the most succinct way to plot such data with basic graphics command in R?

EDIT:

08:00:00    0
08:05:00    .5
08:10:00    1
08:20:00    2
08:30:00    3
08:40:00    4
08:50:00    5
09:00:00    6
09:10:00    7

I tested plot(Y ~ as.POSIXct(time, format="%H:%M:%S"), data = d) using the above data. The resulted figure is below. But the default ticks don't make sense. For hours, it should be split in 6 parts instead of 5 parts. How to make it multiple of 6 by default?

https://www.quora.com/Why-is-the-measurement-of-time-dominated-by-multiples-of-6-e-g-60-seconds-in-a-minute-60-minutes-in-an-hour-24-hours-in-a-day-and-30-days-in-a-month

enter image description here

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 3
    Base R doesn't have a data type that just stored time of date. It has a date+time format. So if you just have hh:mm:ss it's likely being stored as a character vector? This really has to do with your data which you have not included in the question. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 09 '22 at 00:06
  • If you do the conversion you should get reasonable axes: `plot(Y ~ as.POSIXct(time, format="%H:%M:%S"), data = d, xlab = "time")` – MrFlick Mar 09 '22 at 00:12
  • Please unclose it as the updated question is not addressed by the linked questions. – user1424739 Mar 09 '22 at 00:24

0 Answers0