Basically, the remaining issue is that ggplot seems to be using .
instead of : to separate hours and minutes in the labels. I remember
seeing : not too long ago so this is likely the consequence of a
recent change and looks like a bug.
On 2009-June-08 , at 12:35 , Winston Chang wrote:
>>>> I found an alternative solution which might work better for my
>>>> purposes: convert the vector from “times” objects (encoded in
>>>> days) to number of minutes, and write a formatter that prints
>>>> that in a nice format. This way, the automatic breaks will fall
>>>> at convenient locations.
>>>
>>> if you can somehow convert it to POSIXct ggplot deals with the
>>> dates correctly : displays labels at the correct locations and
>>> formats them to show only the part of the date that matters (i.e
>>> only hours, or only days, or only years, etc. depending on the
>>> range of variation on the scale)
>>
>> That seems to work well for the graphs, although it seems to use a
>> "." instead of ":" to separate the numbers.
>> duration <- c("00:34:10", "00:35:10", "00:32:49", "00:34:34",
>> "00:43:24",
>> "00:28:16", "00:34:41", "00:20:53", "00:31:34", "00:35:55",
>> "00:30:50",
>> "00:31:36", "00:30:43", "00:29:55", "00:34:48", "00:32:18",
>> "00:30:47",
>> "00:27:43", "00:28:07", "00:34:54", "00:28:18", "00:24:44",
>> "00:28:13",
>> "00:27:08", "00:34:40", "00:35:06", "00:27:29", "00:34:05",
>> "00:34:22",
>> "00:31:18")
>>
>> dct <- as.POSIXct( strptime(duration, format="%H:%M:%S"))
>>
>> df <- data.frame(dct)
>>
>> df$dct <- as.POSIXct( strptime(duration, format="%H:%M:%S"))
>>
>> # Separator is period instead of colon
>> ggplot(df, aes(x=dct)) + geom_histogram()
>>
>>
>> df$dct
>> # Text output looks like this:
>> #[1] "2009-06-08 00:34:10 CDT" "2009-06-08 00:35:10 CDT"
>> #[3] "2009-06-08 00:32:49 CDT" "2009-06-08 00:34:34 CDT"
>> # ...
>
> that's strange, I remember it being a colon. I guess Hadley changed
> the formatter. You should bring that back to the list (it was a
> mistake on my part to reply to you only in the first place. I meant
> to reply to the list).
>
>> Also, the text display format is a little inconvenient. If it were
>> possible to change the text output as well, that would be ideal.
>
> you can probably get something more convenient with format() for
> some specific output.
>
> If you cant to modify it globally for R, you need redefine a "print"
> method for object of class POSIXt but beware that this can have
> unexpected consequences = it will *show* you the data differently
> but internally, the representation will still include dates etc.
> That's a recipe for headaches down the road, once you forget what
> you did initially. I would advise to define and use this print
> method only locally, for the particular project you are currently
> dealing with.
>
> duration <- c("00:34:10", "00:35:10", "00:32:49", "00:34:34",
> "00:43:24",
> "00:28:16", "00:34:41", "00:20:53", "00:31:34", "00:35:55",
> "00:30:50",
> "00:31:36", "00:30:43", "00:29:55", "00:34:48", "00:32:18",
> "00:30:47",
> "00:27:43", "00:28:07", "00:34:54", "00:28:18", "00:24:44",
> "00:28:13",
> "00:27:08", "00:34:40", "00:35:06", "00:27:29", "00:34:05",
> "00:34:22",
> "00:31:18")
>
> dct <- as.POSIXct( strptime(duration, format="%H:%M:%S"))
>
> print.POSIXt <- function(x) {
> print(format(x, "%H:%M"))
> }
>
> dct
>
> # unload it
> rm("print.POSIXt")
JiHO
---
http://jo.irisson.free.fr/
> Is it possible that you're seeing . used to separate minutes and
> seconds? That's what the code is telling me.
That's indeed the case. It used ":", as expected, before IIRC.
JiHO
---
http://jo.irisson.free.fr/
If it's both H:M and M:S, how do you know which is which?
Hadley
>>> Is it possible that you're seeing . used to separate minutes and
>>> seconds? That's what the code is telling me.
>>
>> That's indeed the case. It used ":", as expected, before IIRC.
>
> If it's both H:M and M:S, how do you know which is which?
Oh right. I did not realize it was MM.SS. I thought it was HH:MM. The
"." as delimiter definitely makes sense in the general case then. Now,
Winston if you absolutely need : instead of . you might need to alter
the code (i.e. redefine the function involved). What would this
function be Hadley? format.POSIXct? I see that scale-datetime.r uses:
labels <- function(.) {
breaks <- .$.tr$inverse(.$input_breaks())
attr(breaks, "tzone") <- .$tz
format(breaks, .$break_points()[3])
}
Hope that helps.
JiHO
---
http://jo.irisson.free.fr/
Nothing that complicated! Just use the format argument to scale_datetime.
Hadley