Rで時系列データの読込み

sentdex.comで配布されてるデータ
sentdex.com/GBPUSD.zip

tb <- read.table("GBPUSD1d.txt",sep=',',colClasses=c("character","numeric","numeric"))
df <- data.frame(strptime(tb$V1,"%Y%m%d%M%S"),tb$V2,tb$V3)
names(df) <- c("time","bid","ask")

こんな感じ

>head(df)
        time     bid     ask
1 2013-05-01 1.55358 1.55371
2 2013-05-01 1.55357 1.55369
3 2013-05-01 1.55353 1.55367
4 2013-05-01 1.55349 1.55367
5 2013-05-01 1.55349 1.55367
6 2013-05-01 1.55348 1.55367

これだとグラフの重ね書きが難しいので悩ましい。

 ggplot(df,aes(x=time,y=ask,bid))+geom_smooth()→askとbidの両方はプロットされない

ふつうのplotだとpar(new=T)で重ね書きできるけど、ggplotではできない。

ggplot(df,aes(x=time,y=ask,bid))+geom_smooth()
par(new=T)→ggplotでは使えない
ggplot(df,aes(x=time,y=bid))+geom_smooth()

こういう時はmeltを使う。

df2 <- melt(df,id="time",measure=c("bid","ask"))

プロット

ggplot(df2,aes(x=time,y=value,col=variable))+geom_line()