2014-01-19 79 views
2

我有兩個分離的數據集:AUD-USDCHF-JPY並且它們看起來像這樣(快照每100毫秒):劇情同一x軸爲兩個不同的數據集

currency, price, datetime 

CHF/JPY, 93.84, 2011-09-06 08:00:00.000 #from 8:00 to 8:30 

AUD-USD, 1.84, 2011-09-06 07:00:00.000 #from 7:00 to 9:00 

mydata$datetime <- as.POSIXct(data$datetime, tz="GMT") 

time1<-as.POSIXct("2011-09-06 7:00:00", tz="GMT") 
time2<-as.POSIXct("2011-09-06 9:00:00", tz="GMT") 
plot(price~datetime, xaxt="n",main="", xlab="Time", ylab="Price", data=mydata) 

我嘗試這樣做:

axis(1,at=seq(time1,time2,by="hour"),label=seq(time1,time2,by="hour")) 

我想有兩個分離的圖與兩個貨幣對相同的X軸來比較它們。

07:00, 07:30, 08:00, 08:30, 09:00 

由於CHF-JPY數據僅從8:00到8:30,我結束了不同的x軸。

對不起,我沒有足夠的知名度發佈圖片。


編輯:

  • dput(mydata)

    structure(list(currency = c("CHF/JPY"), price = c(93.84), Volume = c(1), datetime = structure(c(1315296191.6)))) 
    

回答

0

是否這樣?

set.seed(1) 
df.1 <- data.frame(currency=rep("CHF/JPY",31), 
        price=rnorm(31,60,2), 
        volume=rnorm(31,5e5,1e5), 
        datetime=as.POSIXct("2011-09-06 08:00:00")+seq(0,1800,by=60)) 
df.2 <- data.frame(currency=rep("AUD-USD",121), 
        price=rnorm(121,50,3), 
        volume=rnorm(121,1e6,1e5), 
        datetime=as.POSIXct("2011-09-06 07:00:00")+seq(0,7200,by=60)) 
mydata <- rbind(df.1,df.2) 

library(reshape2) 
library(ggplot2) 
gg <- melt(mydata, id=c(1,4)) 
ggplot(gg) + 
    geom_line(aes(x=datetime, y=value, color=currency)) + 
    stat_smooth(aes(x=datetime, y=value, color=currency), 
       formula=y~1,method="lm", se=F, linetype=2)+ 
    facet_grid(variable~., scales="free") 

編輯針對OP的評論。

ggplot(mydata, aes(x=datetime, y=price, color=currency)) + 
    geom_line()+ 
    stat_smooth(formula=y~1,method="lm", se=F, linetype=2)+ 
    facet_grid(currency~., scales="free") 
+0

謝謝。 1.關於音量,價格在兩個不同的圖表中相同的x軸。您爲價格創建的相同數據集。 2.反正沒有使用ggplot?我會刪除我的帖子中的音量以防止混淆。 – Soheil

+0

看到我上面的修改。 – jlhoward

0

使用dput(my_data)和第一發布您的樣本數據。始終記得發佈樣本數據非常重要,併發布定義明確的問題。那麼只有某人可以回答您的查詢。

+0

2數據集用於兩個不同的貨幣對。數據很大,但所有行的格式與我在這裏描述的相同。 – Soheil

+0

@ User3132179這是比較好的評論。 – hd1

1

繪製其中一個時間序列,如上所示,並使用行(second.obj)添加第二個時間序列。如果這不能排序,請發表評論。回首以上,我希望你進一步編輯你的問題dput(my_data)

+0

我不想同時在一個圖中有兩個數據,兩個不同的圖表具有相同的x軸,謝謝。 – Soheil

相關問題