2015-10-14 64 views
0

我是R的新手,从这个简单的问题出发。我想绘制包含时间值的输入文件的值。输出图像应该包含蓝线(对于Dist为09.0的行)和红线(对于Dist为04.0的行)。从R中的文件绘制时间值的最简单方法是什么?

我设法生成输入整数值直接进入graph.r的图形,但现在我想graph.rinput.txt获取这些值来代替,并让他们格式化为时间(h:mm:ss的)。这是可能的,还是我必须解析input.txt之前喂它到R?


input.txt中

Date(x) Dist Time(y) 
120926 09.0 1:54:42 
121001 04.0 0:19:00 
121202 04.0 0:18:53 
121206 09.0 1:13:19 

graph.r

#!/usr/bin/env Rscript 

# Define the cars vector with 5 values 
input <- 'path/to/input.txt' 
nine <- file(input)    #how to parse it? 
four <- c(3600, 12:00, 16, 4, 9) #this fails because of "12:00" 

# Set output file type to png 
png("output.png", width=320, height=240) 

# Graph using blue points for Dist 9, and red for Dist 4. 
plot(nine, type="o", col="blue") 
plot(four, type="o", col="red") 

# Output to file 
dev.off() 

素描期望的结果,减xy标签和图例

sketch

回答

0

这是怎么了我会去 关于它。您将需要“as.POSIXct”的图形包,并且一旦定位线被执行,您将需要单击位于图例顶角的位置的图。

data=read.table("input.txt",head=T) 

library(graphics) 
plot(data$Date.x.,as.POSIXct(strptime(data$Time.y.,format="%H:%M:%S")),type="n", xlab="Date",ylab="Time") 
lines(data$Date.x.[data$Dist==4],as.POSIXct(strptime(data$Time.y.,format="%H:%M:%S"))[data$Dist==4],col="red") 
lines(data$Date.x.[data$Dist==9],as.POSIXct(strptime(data$Time.y.,format="%H:%M:%S"))[data$Dist==9],col="blue") 

leg=locator(1) 
legend(leg,legend=c(4,9),col=c("red","blue"),lty=1) 
+0

我不知道我很理解你的第二句话。为什么点击?是否不可能以非交互方式执行此操作? – octosquidopus

+0

是的,你不需要使用locator()函数为你的图例选择点,但是你需要手动提供x和y值。 –

+0

如果这对你有用,你能接受答案吗? –

相关问题