我有两个分离的数据集:AUD-USD
并且CHF-JPY
它们看起来像这样(每100毫秒快照):
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))))
像这样?
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")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句