Frage

I have two separated data sets: AUD-USD and CHF-JPY and they look like this (snapshots every 100 milliseconds):

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)

I tried this:

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

I want to have two separated graphs with the same x-axis for both currency pairs to compare them.

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

Since the CHF-JPY data is only from 8:00 to 8:30, I end up with different x-axis.

Sorry, I do not have enough reputation to post images.


EDIT:

  • dput(mydata)

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

Lösung 2

Like this?

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")

Edit In response to OP's comment.

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")

Andere Tipps

Plot one of the timeseries, as you have above and add the second one using lines(second.obj). If this doesn't sort you, leave a comment. Echoing above, I'd like you to further edit your question with a dput(my_data).

use dput(my_data) and post your sample data first. always remember posting sample data is important as well as posting well defined problems. then only someone can answer to your query .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top