R组合相似的行以创建时间序列

时间:2017-07-18 19:23:07

标签: r dataframe plot ggplot2

我正在尝试操纵一些数据以显示使用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

1 个答案:

答案 0 :(得分: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))