I'm tring to plot Time as hh:min
or hh:min:ss
. But as I'm using chron package (it restores time values as a fraction of a day), ggplot don't know how to automatically pick scale for object of type times, so it uses continuous to plot the data. Some sample of the data:
> x$Time
[1] 00:00:00 00:15:00 00:30:00 00:45:00 01:00:00 01:15:00 01:30:00 01:45:00 02:00:00
[10] 02:15:00 02:30:00 02:45:00 03:00:00 03:15:00 03:30:00 03:45:00 04:00:00 04:15:00
> x$Total
[1] 1 4 0 0 4 0 0 0 0 0 0 0 0 0 0 0
[17] 0 0 0 0 2 0 0 0 0 0 0 0 2 0 7 9
[33] 27 87 143 251 1263 1797 2074 2171 16 2542 2924 3194 3185 3274 3282 3362
ggplot(aes(x = x$Hora, y = x$Sum_C.Total), data = x) + geom_point()
This gives a graph where Y axis works, but X axis are values from 0 to 1 (as all my time data are from 00:00:00
to 24:00:00
). How can I convert this fraction of a day in time values in order that ggplot can plot it as hh:mm:ss
or hh:mm
?
PS: I tried this solution (plot chron times as hours from origin) but it doesn't allow me to use the "%02d:00"
format.
I don't know if the following is what you want. if it isn't, just say so and I will delete this answer.
First read in your Time
vector and make up some Total
.
Time <- scan(what = character(), text =
"00:00:00 00:15:00 00:30:00 00:45:00 01:00:00 01:15:00 01:30:00 01:45:00 02:00:00
02:15:00 02:30:00 02:45:00 03:00:00 03:15:00 03:30:00 03:45:00 04:00:00 04:15:00")
set.seed(1)
Total <- cumsum(rnorm(length(Time)))
Now for the graph. I will create a data.frame
with a column of class "times"
and then make it shorter, without the seconds, for the plot.
library(chron)
Time <- chron(times. = Time)
x <- data.frame(Hora = Time, Sum_C.Total = Total)
Hr <- sub(":00$", "", as.character(x$Hora))
ggplot(data = cbind(x, Hr), aes(x = Hr, y = Sum_C.Total)) +
geom_point()
On my screen monitor I had to maximize the graphics window in order for the x axis labels not to superimpose.