2015-06-15 21 views
1

在下面的圖中,我想刪除日期並以小時爲單位保留時間。我看到了一些解決方案,如here和另一個時間轉換爲數字的解決方案。我喜歡使用require(scales)ggplot2中以小時爲單位的縮放時間

ggplot(data= dfweek2, aes(x = Time, y = Mean_steps)) + facet_grid(day ~.) + geom_path() 

這是我的陰謀 enter image description here

當我改變 require(scales) ggplot(data= dfweek2, aes(x = Time, y = Mean_steps)) + facet_grid(day ~.) + geom_path() + scale_x_datetime(labels = date_format("%H:%M"))

這個規模不再正確... enter image description here

我是什麼做錯了?

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

回答

3

我不知道這是否是一個時區的東西。查看attributes(dfweek2$Time)的輸出並查看它是否具有tzone屬性。如果是這樣,那麼當format被稱爲沒有tz參數時,它將轉換它們存儲的任何時區的時間以及您設置的任何系統時區。如果是這種情況,你有兩種選擇。

  1. dfweek2$Time

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

  2. 卸下tzone屬性提供與tz參數格式化。例如,如果您發現dfweek2$Time的時區是 「EST」,則:

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

+0

感謝尼克。兩者都是有見地的解決方案我還發現我沒有設置'tz'參數,因此它使用本地tz,而'date_format'使用'GMT'作爲默認值。因此,這個問題。 – pdm

相關問題