Product: TIBCO Spotfire®
How can I add a vertical line to a timeSeries plot?
User would like to add a vertical line to a timeSeries() plot:
There are different methods to add a vertical line at a specific date depending on which plotting function you are using. Here are two examples using the plot.timeSeries() function and the tsplot() function.
1. Using plot.timeSeries() function:
You can use the abline() function to add vertical lines to your plot.timeSeries() graph. One important thing to note is that the x-axis plot coordinates of a time series plot range from 0 to 1, not from the start date to the end date. Please see the following code for an example of how to do this using the built-in dataset djia:
## plot the Dow Jones Industrial Average between Sept 1987 and Sept 1989
> djia1 <-djia[positions(djia)>=timeDate("09/01/87") &
positions(djia)<=timeDate("09/01/89"), 1:4]
# Plot the timeSeries
# I set reference.grid argument to False so that the
# reference grid lines did not plot
> ts.out=plot.timeSeries(djia1, plot.type="hloc", reference.grid=F)
#Get the x-axis tick values from the plot.timeSeries() plot
> x1 <- ts.out$ticks$small$at
### You can also get x1 like this which creates a time sequence ###
### x1 <- timeSeq(from=ts.out$scale$user.coordinates[1], to=ts.out$scale$user.coordinates[2], by="months") ###
# Divide the x-axis into the length of the x1 sequence to
# get the coordinates of the tick marks
> x2 <- seq(0,1,length=length(x1))
#Plot the vertical lines using abline() where
#the x-axis label is Q1 (Jan 1st)
#the lwd argument makes the line thicker
> abline(v=c(x2[months(x1)=="Jan" & days(x1)=="1"]), lwd=6)
2. Using ts.plot() function:
The ts.plot() function works differently and allows you to pass the year to abline:
> ts.plot(hstart)
> abline(v = 1973)
References: You can view the help files for any of the functions used above by typing a question mark followed by the function name at the S+ command prompt:
> ?plot.timeSeries
> ?timeSeq
> ?seq
> ?par #(to view information on the lwd argument)
> ?abline
> ?ts.plot
Comments
0 comments
Article is closed for comments.