2017-07-18 65 views
0

我想操縱一些數據來顯示使用R的時間序列的汽車。我想繪製每個汽車在時間序列上的速度。我希望它看起來像下面的圖片。每條線代表不同的汽車。R結合相似的行來創建時間序列

Cars Date Speed 
Yellow Car 2017-07-02 17:41:00 20 
Green car 2017-07-08 05:01:35 30 
Yellow Car 2017-07-08 05:03:31 10 
Blue Car 2017-07-08 05:52:55 4 
Green car 2017-07-08 10:21:57 2 
Green car 2017-07-08 12:07:51 12 

How I want my data to look like

回答

0

你的數據集是不完整的,但你可以從下面的範例中得到啓發:

ggplot(df, aes(x=Time, y=Speed, group=Cars, color=Cars)) + geom_line() + 
       scale_colour_manual(values=c("blue", "green", "yellow")) 

enter image description here

數據:

這與您的數據不同(它有不同的列),但看起來像它。

df <- structure(list(Cars = structure(c(3L, 2L, 3L, 1L, 2L, 1L), .Label = c("BlueCar", 
"Greencar", "YellowCar"), class = "factor"), Date = structure(c(1L, 
1L, 1L, 1L, 1L, 2L), .Label = c("2017-07-08", "207-07-08"), class = "factor"), 
Time = structure(c(6L, 1L, 2L, 3L, 4L, 5L), .Label = c("05:01:35", 
"05:03:31", "05:52:55", "10:21:57", "12:07:51", "17:41:00" 
), class = "factor"), Speed = c(20L, 30L, 10L, 4L, 2L, 15L)), 
.Names = c("Cars", "Date", "Time", "Speed"), class = "data.frame", row.names = c(NA,-6L)) 
+0

老兄,這真棒幫助!還有一個問題,如果我想在x軸中表示日期和時間,該怎麼辦?我偶然發現as.POSIXlt似乎採用這種UTC格式,我如何在這裏實現它? – AnthonyM

+0

@AnthonyM添加這個'+ scale_x_datetime(breaks = Date,labels = format(Date,「%Y-%m-%d%H:%M:%S」)' – Masoud