Reputation: 57
In the following plot I wanted to remove the date and keep the time in hours. I have seen few solutions like here and another where Time is converted to numeric. I liked the use of require(scales)
ggplot(data= dfweek2, aes(x = Time, y = Mean_steps)) + facet_grid(day ~.) + geom_path()
this is my plot
When I change to
require(scales)
ggplot(data= dfweek2, aes(x = Time, y = Mean_steps)) + facet_grid(day ~.) + geom_path() + scale_x_datetime(labels = date_format("%H:%M"))
this scale is not longer correct...
What am I doing wrong?
> str(dfweek2)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 576 obs. of 4 variables:
$ day : Factor w/ 2 levels "Weekday","Weekend": 1 1 1 1 1 1 1 1 1 1 ...
$ interval : int 0 5 10 15 20 25 30 35 40 45 ...
$ Mean_steps: num 2.333 0.462 0.179 0.205 0.103 ...
$ Time : POSIXct, format: "2015-06-15 00:00:00" "2015-06-15 00:05:00" ...
Upvotes: 3
Views: 2530
Reputation: 12640
I wonder if it's a timezone thing. Look at the output of attributes(dfweek2$Time)
and see if it has a tzone
attribute. If it does, then when format
is called without a tz
parameter, it will convert the times from whatever time zone they're stored as to whatever system time zone you've got set. If this is the case, you have two options.
Remove the tzone attribute from dfweek2$Time
attr(dfweek2$Time, "tzone") <- NULL
Provide the formatter with the tz
parameter. For example, if you find the time zone of dfweek2$Time
is "EST", then:
scale_x_datetime(labels = function(x) format(x, "%H:%M", tz = "EST"))
Upvotes: 3