Skip to content

plotting 2 time series data on the same graph

4 messages · Preetam Pal, Rui Barradas, arun

#
Hello,

Where is the reproducible example? See

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example


Anyway, see if this does what you need.

set.seed(1)
x <- cumsum(rnorm(100))
y <- cumsum(rnorm(100))

ylm <- range(c(x, y))
plot(as.ts(x), ylim = ylm)
lines(y, col = "blue")

# Or in one instruction
matplot(cbind(x, y), type = "l")


Hope this helps,

Rui Barradas


Em 04-05-2013 10:21, Preetam Pal escreveu:
#
Hello,

I forgot a third possibility


plot(as.ts(cbind(x, y)), plot.type = "single", col = 1:2)


Rui Barradas

Em 04-05-2013 10:37, Rui Barradas escreveu:
#
Hi,
One more possibility:
dateA<-seq.Date(as.Date("1jan2012",format="%d%b%Y"),as.Date("14Feb2013",format="%d%b%Y"),by="day")
?dateB<-seq.Date(as.Date("1Mar2012",format="%d%b%Y"),as.Date("30Nov2012",format="%d%b%Y"),by="day")
set.seed(15)
?A<-data.frame(dateA,value=cumsum(rnorm(411)))
?set.seed(25)
?B<-data.frame(dateB,value=cumsum(rnorm(275)))
library(xts)
Anew<-as.xts(A[,-1],order.by=A[,1])
?Bnew<-as.xts(B[,-1],order.by=B[,1])
?res<-merge(Anew,Bnew)
res1<-res[complete.cases(res),]
#install.packages("xtsExtra", repos='http://r-forge.r-project.org')
library(xtsExtra)
plot(res,screens=1,main="Time Series")
plot(res1,col=c(1,3))
A.K.



----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Preetam Pal <lordpreetam at gmail.com>
Cc: r-help at r-project.org
Sent: Saturday, May 4, 2013 5:48 AM
Subject: Re: [R] plotting 2 time series data on the same graph

Hello,

I forgot a third possibility


plot(as.ts(cbind(x, y)), plot.type = "single", col = 1:2)


Rui Barradas

Em 04-05-2013 10:37, Rui Barradas escreveu:
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TimeSeries.pdf
Type: application/pdf
Size: 11356 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130504/09923735/attachment.pdf>