简体   繁体   English

在X轴上绘制带有时间戳的线图

[英]Drawing line graphs with time stamps in X-axis

I have a below data set. 我有一个以下的数据集。

TimeStamp   data_1      data_2      data_3      
15:11:37.474    99.791028   0.312498    99.47853    
15:16:22.373    99.791028   0.729162    99.061866   
15:21:37.424    99.791028   0.104166    99.686862   
15:31:52.475    88.02027            90.520254   
15:42:07.157    99.99936    0.208332    99.791028   
15:43:22.279    99.99936    0.52083     99.47853    
15:45:37.673    99.686862   0       99.686862   
15:52:52.872    99.686862   0.729162    98.9577 

I require to draw simple line graphs as below.But, all should be sharing same X and Y axis. 我需要绘制如下的简单线图。但是,所有应该共享相同的X和Y轴。 For timestamp, sampling with 5 minutes is fine. 对于时间戳,5分钟的采样是好的。

data_1 vs TimeStamp
data_2 vs TimeStamp
data_3 vs TimeStamp

What I have tried so far is as below. 到目前为止我尝试的内容如下。

df <- read.csv(file="c:\\td.csv")

df1 <- round(df[-1], 1)

ggplot(df, aes(df$id, rdat$f1)) + geom_point()

I know this is not the right approach and any help is appreciated. 我知道这不是正确的方法,任何帮助表示赞赏。

It seems that you want something like the following. 您似乎想要以下内容。 You want to convert your data in a wide format to a long format. 您希望将宽格式的数据转换为长格式。 I used melt() , but you can use gather() in the tidyr package too. 我使用了melt() ,但你也可以在tidyr包中使用gather() From there, you just create a graphic. 从那里,你只需创建一个图形。 I do not know what you meant by saying "For timestamp, sampling with 5 minutes is fine." 我不明白你的意思是说“对于时间戳,用5分钟取样就好了。” If you need further data manipulation, this will help you proceed further. 如果您需要进一步的数据操作,这将有助于您进一步。

library(reshape2)
library(ggplot2)

temp <- melt(mydf, id.vars = "TimeStamp", variable.name = "data")

ggplot(data = temp, aes(x = TimeStamp, y = value, color = data,
       group = data)) +
geom_line() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

在此输入图像描述

DATA 数据

mydf <- structure(list(TimeStamp = structure(1:8, .Label = c("15:11:37.474", 
"15:16:22.373", "15:21:37.424", "15:31:52.475", "15:42:07.157", 
"15:43:22.279", "15:45:37.673", "15:52:52.872"), class = "factor"), 
data_1 = c(99.791028, 99.791028, 99.791028, 88.02027, 99.99936, 
99.99936, 99.686862, 99.686862), data_2 = c(0.312498, 0.729162, 
0.104166, NA, 0.208332, 0.52083, 0, 0.729162), data_3 = c(99.47853, 
99.061866, 99.686862, 90.520254, 99.791028, 99.47853, 99.686862, 
98.9577)), .Names = c("TimeStamp", "data_1", "data_2", "data_3"
), class = "data.frame", row.names = c(NA, -8L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM