2017-05-10 21 views
0
d=read.table(text="in_type, in_time, out_type, out_time, d2d, e2e 
    R1,14:56:04.434285,R2,14:56:04.434534,152,249 
    R1,14:56:04.522163,R2,14:56:04.522325,113,162 
    R1,14:56:04.606073,R2,14:56:04.606228,112,155 
    R1,14:56:04.824225,R2,14:56:04.824391,116,166 
    R1,14:56:06.621347,R2,14:56:06.621511,116,164 
    R1,14:56:37.677250,R2,14:56:37.677452,135,202 
    R1,14:56:38.897656,R2,14:56:38.897839,123,183 
    R1,14:56:50.361073,R2,14:56:50.361268,127,195 
    R1,14:59:09.768824,R3,14:59:09.769006,138,182 
", sep=',', header=T) 
d=d[order(d$in_time),] 
d$in_s = strptime(substr(d$in_time,1,8),"%H:%M:%S") 
plot(d$in_s,d$d2d) 

plot(d$in_s,d$d2d, xlim=c('14:56:06','14:56:37')) 
Error in plot.window(...) : invalid 'xlim' value 

class(d$in_s) # will get POSIXlt 
plot(d$in_s,d$d2d, xlim=as.POSIXlt(c('14:56:06','14:56:37', format="%H:%M:%S")) 
Error in plot.window(...) : invalid 'xlim' value 
+2

你的'xlim'值是文本 - 'c('14:56:06','14:56:37')'。他們必須是數字範圍。您的'd'也沒有正確分配標題。張貼之前請檢查您的示例。 – thelatemail

回答

1

你可以使用GGPLOT2如:

library(ggplot2) 
library(scales) 
ggplot(d) + geom_line(aes(x = in_s,y = d2d)) + 
    scale_x_datetime(limits = as.POSIXct(c("14:56:06", "14:56:37"), format = "%H:%M:%S"), breaks=date_breaks("1 secs"), labels=date_format("%H:%M:%S"))+ 
theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

enter image description here

+0

我使用下面的代碼,但我希望x標籤是%H:%M:%S格式,'ggplot(d)+ geom_point(aes(x = in_s,y = d2d))+ xlim(as.POSIXct c('14:56:06','14:56:37'),format =「%H:%M:%S」))' –

+0

@DanielYCLin請參閱我的編輯 –

+0

找不到函數'date_breaks ' –

1

首先,你需要一個

頭= T

來讀取你的數據。對於

XLIM

它不能直接拿串。你需要格式化它。下面的代碼應該適合你:

d=read.table(text="in_type, in_time, out_type, out_time, d2d, e2e 
       R1,14:56:04.434285,R2,14:56:04.434534,152,249 
       R1,14:56:04.522163,R2,14:56:04.522325,113,162 
       R1,14:56:04.606073,R2,14:56:04.606228,112,155 
       R1,14:56:04.824225,R2,14:56:04.824391,116,166 
       R1,14:56:06.621347,R2,14:56:06.621511,116,164 
       R1,14:56:37.677250,R2,14:56:37.677452,135,202 
       R1,14:56:38.897656,R2,14:56:38.897839,123,183 
       R1,14:56:50.361073,R2,14:56:50.361268,127,195 
       R1,14:59:09.768824,R3,14:59:09.769006,138,182", sep=',',header=T) 
    d=d[order(d$in_time),] 
    d$in_s = strptime(substr(d$in_time,1,8),"%H:%M:%S") 

    plot(d$in_s,d$d2d, xlim=as.POSIXct(c("14:56:06", "14:56:37"), format = "%H:%M:%S")) 
+1

x軸標籤似乎不見了。 –

+0

我很奇怪爲什麼類(d $ in_s)是POSIXlt,爲什麼不能只使用as.POSIXlt? –

+0

如果您看?xlim:「長度爲2的數字向量,給出x和y座標範圍」。您可以將axis.POSIXct用於HH:MM:SS,但您可能需要先將數據集分組。 –

相關問題