2013-02-11 78 views
1

我對ggplot2非常陌生,想知道是否有人可以幫我解決我的簡單問題。我的示例數據框如下所示。我想根據計數數量繪製時間(小時和分鐘)。使用ggplot2繪製時間序列的錯誤消息

Date <- c("07/12/2012 05:00:00", "07/12/2012 06:00:00", "07/12/2012 07:00:00", 
     "07/12/2012 08:00:00") 
Date <- strptime(Date, "%d/%m/%Y %H:%M") 
Counts <- c("0","3","10","6") 
Counts <- as.numeric(Counts) 
df1 <- data.frame(Date,Counts,stringsAsFactors = FALSE) 

#Adds time to dataframe. 
df1 <- within(df1,{ 
    posb <- as.POSIXlt(Date,format="%d/%m/%Y %H:%M") 
hours <- posb$hour 
mins <- posb$min 
dates <- format(posb, "%x") 
time <- format(posb, "%H:%M") 
posb <- NULL # cleanup 
}) 
#Draw graph (time versus Counts) 
library(ggplot2) 
g = ggplot(df1, aes(x=time, y=Counts)) 
g + geom_line() 

我總是得到錯誤消息'geom_path:每個組只包含一個觀察值。你需要調整團體審美嗎?'。

任何人都可以幫我糾正我的代碼,讓我的圖正確繪製?

編輯 我仍然試圖讓我的時間變量的格式工作,並繪製圖。但目前,它還沒有識別日期格式。 我希望能夠: 1.繪製一段時間的數據(比如從07:47:50到07:49:10)。 2.詢問R每整分鐘繪製x軸。 ......我現在都無法工作。下面顯示了我真實數據的一個子集。任何意見將受到感謝。

day3 <- structure(list(Date = c("11/12/2012", "11/12/2012", "11/12/2012", 
          "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", 
          "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", 
          "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", 
          "11/12/2012", "11/12/2012"), Time = c("07:46:10", "07:46:20", 
                    "07:46:30", "07:46:40", "07:46:50", "07:47:00", "07:47:10", "07:47:20", 
                    "07:47:30", "07:47:40", "07:47:50", "07:48:00", "07:48:10", "07:48:20", 
                    "07:48:30", "07:48:40", "07:48:50", "07:49:00", "07:49:10", "07:49:20" 
          ), Axis1 = c(59L, 651L, 59L, 0L, 22L, 50L, 0L, 0L, 114L, 899L, 
             129L, 33L, 21L, 9L, 224L, 135L, 266L, 16L, 59L, 126L), Steps = c(1L, 
                              2L, 1L, 0L, 2L, 1L, 0L, 0L, 5L, 15L, 6L, 2L, 2L, 0L, 8L, 5L, 
                              16L, 1L, 3L, 8L)), .Names = c("Date", "Time", "Axis1", "Steps" 
                             ), row.names = 52838:52857, class = "data.frame") 
#Creates a new dataframe with a time column. 
day3 <- within(day3,{ 
    posb <- as.POSIXlt(Time,format="%H:%M:%S") 
    posb <- NULL # cleanup 
}) 

library(ggplot2) 
g = ggplot(day3, aes(x=strptime(Time, "%H:%M:%S"), y=Axis1)) + geom_line(aes(group = 1)) + 
    theme_bw() + 
    xlab("Time") + 
    ylab("Activity (Counts per 10 seconds)") + 
    scale_x_datetime(limits=c(as.POSIXct("07:47:50"),as.POSIXct("07:49:10"))) 


g 

回答

1

的問題是由於這樣的事實,你的time變量是一個字符向量:

R> class(df1$time) 
[1] "character" 

你必須將其convet到POSIXlt類的對象,例如像這樣:

ggplot(df1, aes(x=strptime(time, "%H:%M"), y=Counts)) + geom_line() 

或者更簡單一些,你可以直接使用你的Date變量,不用轉換:

ggplot(df1, aes(x=Date, y=Counts)) + geom_line() 

enter image description here

更重要的是,你會看到,ggplot2取決於沿軸的時間跨度自動將標籤的x軸。

編輯:如果要定義x軸的限制,你可以這樣做:

ggplot(df1, aes(x=Date, y=Counts)) + geom_line() + scale_x_datetime(limits=c(as.POSIXct("2012/12/07 04:00:00"),as.POSIXct("2012/12/07 10:00:00"))) 
+0

非常感謝@Juba。再一個簡單的問題。因爲我有時間,重新定義x軸的最佳方式是什麼(我想從04:00到10:00)。我想我應該使用:xlim = c(xmin,xmax)),但我努力讓這個工作... – 2013-02-11 16:03:52

+0

@ KT_1我編輯我的答案來添加一種方法來做到這一點,但肯定有一些更簡單。 – juba 2013-02-11 17:16:29

+0

非常感謝@juba。我仍在努力讓代碼與我的真實數據一起工作。我編輯了我的原始問題以顯示額外的數據。任何幫助將是最令人滿意的。 – 2013-02-18 13:45:21