Scaling Time in Hours in ggplot2

时间:2015-06-15 14:59:42

标签: r ggplot2

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 enter image description here

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...enter image description here

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" ...

1 个答案:

答案 0 :(得分:3)

我想知道它是否属于时区。查看attributes(dfweek2$Time)的输出,看看它是否具有tzone属性。如果是,那么当没有format参数调用tz时,它会将时间从他们存储的任何时区转换为您设置的任何系统时区。如果是这种情况,您有两种选择。

  1. dfweek2$Time

    中删除tzone属性

    attr(dfweek2$Time, "tzone") <- NULL

  2. 为格式化程序提供tz参数。例如,如果您发现dfweek2$Time的时区是&#34; EST&#34;,则:

    scale_x_datetime(labels = function(x) format(x, "%H:%M", tz = "EST"))