Reputation: 1355
I am trying to plot the Operating Cash flow of BBBY (Bed Bath & Beyond) and the stock price.
2x Problem! 1) The price variable is daily, and operating cash flow variable I have is annual. When I plot both charts the graph never lines up correctly. How do I plot two time series, of different frequencies and have the annual operating cash flow data points line up to the daily variable on the chart. 2) How do I get the axis for operating cash flow only on the right of the chart and the stock price only on the left. Everytime I try to graph both variables the operating cash flow gets pasted on top of the existing stock price and you end up with a jumbled left axis. Help!!! (Thank you )
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
library("quantmod")
getSymbol("BBBY",from="1-1-2000")
data1=fund.data("BBBY",30,"annual",1)
operatingcash=as.numeric(gsub(",","",data[169,]))
date.year=seq(as.Date("1994-01-01"),length=22,by="years")
plot(BBBY)
par(new=T)
axis(4)
plot(date.year,operatingcash,type="l",axis=4)
Upvotes: 1
Views: 825
Reputation: 94162
First, plot the main series:
plot(BBBY)
Then reset the Y limits by changing values 3 and 4 in par()$usr
:
par(usr="[<-"(par()$usr,3:4,range(operatingcash)))
Now add the operating cash. Note conversion to as.POSIXct
since that's what the time
series plot sets the x-axis with:
lines(as.POSIXct(date.year),operatingcash,col="red")
And now add the right axis and label:
axis(4,col="red",col.axis="red")
mtext(4,col="red",text="Operating Cash",line=3)
Giving:
The operating cash line is now chopped because its data goes back to 1994. You can also see that the data points line up with the year starts.
The only change to your example I did was to call the data data
and not data1
, and use getSymbols
and not getSymbol
.
Upvotes: 2