2017-07-07 90 views
-1

我有,我已經添加了小時到我的約會結束scale_x_date其他列或使用時間以及日期

數據的數據幀:

structure(list(date = c("2016-01-30 11", "2016-01-30 13", "2016-01-30 16", 
    "2016-01-30 18", "2016-01-30 21", "2016-01-31 2", "2016-01-31 5", 
    "2016-01-31 7", "2016-01-31 13", "2016-01-31 13"), Obs = c(9L, 
    15L, 15L, 16L, 15L, 16L, 15L, 16L, 14L, 9L), score = structure(c(3L, 
    1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 2L), .Label = c("1", "1.5", "2" 
    ), class = "factor"), date2_1 = c("2016-01-30", "2016-01-30", 
    "2016-01-30", "2016-01-30", "2016-01-30", "2016-01-31", "2016-01-31", 
    "2016-01-31", "2016-01-31", "2016-01-31"), date2_2 = c(11L, 13L, 
    16L, 18L, 21L, 2L, 5L, 7L, 13L, 13L)), .Names = c("date", "Obs", 
    "score", "date2_1", "date2_2"), class = c("data.table", "data.frame" 
    ), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x00000000001f0788>) 

我的情節是:

ggplot(data = tt, aes(date2_1, Obs, colour=score)) + 
     geom_point() + 
     scale_colour_discrete(drop=TRUE, limits = levels(t$score)) 

在另一個數據幀I只具有日期(這是當量在這個數據date2_1)和更改與as.Date使得下列作品類:

ggplot(data = t, aes(date, Obs, colour=score)) + 
     geom_point() + 
     scale_x_date(date_breaks = "1 day", date_labels="%d") + 
     scale_colour_discrete(drop=TRUE, limits = levels(t$score)) 

我想對我展示了第一幅圖使用scale_x_date(date_breaks = "1 day", date_labels="%d"),但在使用date2_1代替date規模。

額外:在我的示例數據中,更好的方法是使用scale從date運行,但是這不是課程日期,因爲我將小時粘貼到它上面。這個數據框是另一個子集,我確實有一個日期時間列,其值爲2016-01-30 11:50:52,它們是POSIXct,如果這有幫助的話 - 實際上我使用cSplit,然後粘貼來獲取我的日期樣本數據。使用as.POSIXctstrptime我無法創建一個只有一小時的日期,或者如果我沒有scale_x_date就不能使用它。

本質上我有一個只能縮放井的日期的情節,但是在使用日期和小時時,日期時間均勻分佈在x軸上,而不考慮日期和小時。

對這兩個選項的答案將不勝感激!

回答

1

不清楚你遇到的問題是什麼,也不是你想要的是什麼,但希望這至少可以讓你指向正確的方向。

你問的第一個選項是在x軸使用一天date2_1,我沒有看到轉變date2_1Date任何問題:

library(ggplot2) 

ggplot(df, aes(as.Date(date2_1), Obs, color = score)) + 
    geom_point() + 
    scale_x_date('Date', 
       date_breaks = 'day', 
       date_labels = '%d') 

第二個選項取小時在date列考慮到,所以我們必須將其轉換爲POSIXct,使用正確的格式(注意使用scale_x_datetime代替scale_x_date):

library(dplyr) 
library(ggplot2) 

df %>% 
    mutate(date = as.POSIXct(date, format = '%Y-%m-%d %H')) %>% 
    ggplot(aes(date, Obs, color = score)) + 
    geom_point() + 
    scale_x_datetime('Date', 
        date_breaks = '6 hours', 
        date_labels = '%d h:%H') 

數據:

df <- structure(list(date = c("2016-01-30 11", "2016-01-30 13", "2016-01-30 16", 
           "2016-01-30 18", "2016-01-30 21", "2016-01-31 2", "2016-01-31 5", 
           "2016-01-31 7", "2016-01-31 13", "2016-01-31 13"), Obs = c(9L, 
                         15L, 15L, 16L, 15L, 16L, 15L, 16L, 14L, 9L), score = structure(c(3L, 
                                          1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 2L), .Label = c("1", "1.5", "2" 
                                         ), class = "factor"), date2_1 = c("2016-01-30", "2016-01-30", 
                                                  "2016-01-30", "2016-01-30", "2016-01-30", "2016-01-31", "2016-01-31", 
                                                  "2016-01-31", "2016-01-31", "2016-01-31"), date2_2 = c(11L, 13L, 
                                                                16L, 18L, 21L, 2L, 5L, 7L, 13L, 13L)), .Names = c("date", "Obs", 
                                                                            "score", "date2_1", "date2_2"), class = c("data.table", "data.frame" 
                                                                            ), row.names = c(NA, -10L)) 

(請注意,我不得不從您的dput刪除.internal.selfref = <pointer: 0x00000000001f0788>

+0

不知道爲什麼我沒有看到之前scale_x_datetime。 TY – Olivia

相關問題