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" ...
答案 0 :(得分:3)
我想知道它是否属于时区。查看attributes(dfweek2$Time)
的输出,看看它是否具有tzone
属性。如果是,那么当没有format
参数调用tz
时,它会将时间从他们存储的任何时区转换为您设置的任何系统时区。如果是这种情况,您有两种选择。
从dfweek2$Time
attr(dfweek2$Time, "tzone") <- NULL
为格式化程序提供tz
参数。例如,如果您发现dfweek2$Time
的时区是&#34; EST&#34;,则:
scale_x_datetime(labels = function(x) format(x, "%H:%M", tz = "EST"))