2014-02-28 112 views
2

我有以下的數據幀:劇情時間序列(24小時)中的R

> head(my[,1:2]) 
      Time TD_Wait 
96 18:24:45.776 12442 
97 18:24:53.798 26799 
1944 19:10:32.963 14423 
1945 19:10:34.709 13592 
1946 19:10:38.056 13457 
1947 19:10:38.281 14063 

> str(my) 
'data.frame': 25007 obs. of 2 variables: 
$ Time : Factor w/ 253251 levels "00:00:00.586",..: 27991 28001 33296 33306 33319 33320 33341 33363 33383 33392 ... 
$ TD_Wait: int 12442 26799 14423 13592 13457 14063 11717 10026 10590 19372 ... 

我無法格式化在X軸的時間標記,當我嘗試

ggplot(data=my, aes(x=my$Time,y=TD_Wait/1000)) + geom_point() 

因爲所有的時間都是相互重疊的。我很困惑如何繼續這個。我試圖改變時間的因素,但沒有得到它。

有沒有辦法在24小時內按小時顯示X軸上的時間標籤?

回答

4

轉換爲第一POSIXct:

my$Time <- as.POSIXct(my$Time, format="%H:%M:%S") 
ggplot(data=my, aes(x=my$Time,y=TD_Wait/1000)) + geom_point() 

enter image description here

ggplot負責剩下的照顧。

+0

真棒,我這麼笨我與POSIXct玩弄但是不成功....非常感謝你@BrodieG – user3006691

+0

@ user3006691,很高興這有助於。如果這回答你的問題,請考慮將其標記爲已回答。 – BrodieG

+0

完成並再次感謝:)! – user3006691

1

這是base圖形方法。如圖所示,您可以使用axis.POSIXctaxis.Date繪製POSIX*Date軸。

d <- read.table(text='Time TD_Wait 
18:24:45.776 12442 
18:24:53.798 26799 
19:10:32.963 14423 
19:10:34.709 13592 
19:10:38.056 13457 
19:10:38.281 14063', header=TRUE) 

d$Time <- as.POSIXct(d$Time, format="%H:%M:%S") 
plot(d$Time, d$TD_Wait/1000, xaxt='n', pch=20, las=1, 
    xlab='Time', ylab='TD_Wait/1000') 
axis.POSIXct(1, d$Time, format="%H:%M:%S") 

enter image description here