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)
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.
Related
While doing back testing on daily time frame I wish to refer Weekly (HTF) macd line value. The default MACD indicator in tradingview doesn't show value nor does it plot macdline of higher HTF.
So i wrote following code which shows the value but doesn't plot the line while week is running. It plots the line only after week gets over. During back testing, how do I plot the running value of weekly MACD while week is still on?
macdlineH = request.security(syminfo.tickerid, "W", macdline[0])[0]
plot(macdlineH, title='MACD', color=color.new(color.blue, 0), linewidth=2)
I have a data frame (df) that contains time (five min intervals) and number of animals that passes by at three different locations. Countings starts and ends at the same time for all three places. They are passing by the three places at different times; first place1, second place2 and third place3 due to their alignment in space.
Today I get:
Number of animals on the y-axis and time on the x-axis (where there are three separate groupings like I want). However, time starts over again for each of the three groupings (thus the x-axis looks like this (start-end, start-end, start-end).
What I want:
A plot with number of counts on the y-axis, and time on the x-axis (from start to end of counting). Thus there will be a "displacement" of number of animals (possible fewer as well) as time goes by.
My code today is:
barplot(df$nr.animals, names.arg = df$time, xlab="Time", ylab="Number of animals counted")
How can I get this data into a barplot with time on the x-axis (without the time starting at the beginning again for each new place)?
EDITED:
see picture for some of my dataset:
time=time, pos has three different values: mmys1, mmys_c1 and mmys_c2, which stands for the three different places, nr.bats.dir1 = number of animals counted in that specific time interval
see picture of the structure of my dataset:
time has factor format, nr,bats.dir1 has integer format
thus my code is actually like this
barplot(df$nr.bats.dir1, names.arg = df$time, xlab="Time", ylab="Number of bats counted")
Thanks!
I want to combine a time series of in situ values (line) with boxplots of estimated values of special dates. I tried to understand this "Add a line from different result to boxplot graph in ggplot2" question, but my dates make me drive crazy. Sometimes I only have in situ values of a date, sometimes only estimated values and sometimes both together.
I uploaded a sample of my data here:
http://www.file-upload.net/download-9942494/estimated.txt.html
http://www.file-upload.net/download-9942495/insitu.txt.html
How can I create a plot with both data sets that looks like this http://www.file-upload.net/download-9942496/desired_outputplot.png.html
in the end?
I got help and have a solution now:
insitu <- read.table("insitu.txt",header=TRUE,colClasses=c("Date","numeric"))
est <- read.table("estimated.txt",header=TRUE,colClasses=c("Date","numeric"))
insitu.plot <- xyplot(insitu~date_fname,data=insitu,type="l",
panel=function(x,y,...){panel.grid(); panel.xyplot(x,y,...)},xlab=list(label="Date",cex=2))
est.plot <- xyplot(estimated~date,data=est,panel=panel.bwplot,horizontal=FALSE)
both <- insitu.plot+est.plot
update(both,xlim=range(c(est$date,insitu$date_fname))+c(-1,1),ylim=range(c(est$estimated,insitu$insitu)))
I have a time series over 10 years with no seasonal changes (only one value per year) and I try to detect a trend. I dont really understand how to do so.
I read that the moving average is used in this case
What I did so far:
CharFS <- read.csv("./DataInvestigation/TEST.csv", header=TRUE, sep=";")
ma.1 <- rep(1/5,5)
ma.2 <- rep(1/25,25) #How do I know what I should take?
ma.3 <- rep(1/81,81)
CharMA <- filter(CharFS, ma.1, sides=2)
plot(CharFS)
lines(ma.1, lty=2, col="blue")
lines(ma.2, lty=2, col="blue")
lines(ma.3, lty=2, col="blue")
However, no line shows up. I assume that ma.1,ma.2,ma.3 are wrong is but I dont know how to adjust it to my data, any ideas?
Or is there a better way to get a trend when there are no seasons involved? Would it be possible with a normal plot and then add a line? This did not work either when I tried it though.
Thanks in advance!
It would be useful if you could show us the plot of your data with the moving averages lines. Or maybe provide some data.
The reason they don't show up in the plot might have something to do with range of the data you use. What is the mean of CharFS? Try to plot CharFS and then add a horizontal line for the mean of the time series.
With regard to trends in time series data: basically when you have a trend in the data you will see that the data moves in a certain direction (e.g. GDP figures) and does not revolve around a certain mean (e.g. GDP growth). See the two examples that I have plotted below.
Since you're using R this might be a helpful resource using time series data: Little Book of R for Time Series
I have a time series of zoo and POSIXct class using second intervals. However, as is usually the case in financial time series, there are often long time gaps without information (ex. overnight). When using plot.zoo, the useful (populated) intervals appear scrunched relative to the unused intervals. Is there some way to simply omit time intervals in the plotting function of plot.zoo(). P.S. It's possible to just simply plot the continuous time series (plot(ts(obj)), but then the time information is lost on the x axis.
#AndresT Thanks for the idea, but the data is already filtered. It is the plot.zoo() function that appears to be padding unused sample intervals to keep the total x range
active. Pls see example below.
library(zoo)
st<-Sys.time()
t<-c(st, st+500, st+1000, st+1500, st+2000, st+90000, st+95000, st+100000)
rn<-runif(7)
zr<-zoo(rn, order.by=t)
plot.zoo(zr,type='o')
compare to the uniform spacing from plot(ts(data))
layout(1:2)
plot.zoo(zr, type='o')
plot(ts(zr),type='o')
So I would like to basically have the 2nd ts plot, but labeled with the corresponding x-axis labels from the 1st plot.zoo() function.
Try this:
library(quantmod)
chartSeries(zr)