Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding points to plot.ts, when start, end and frequency are set

I have problem with adding points to plot.ts. If I create ts object as in example below, everything is fine and points are added to the plot.

data <- ts(rnorm(100))
plot(data)
ind <- c(1,10,67)
points(ind, data[ind], pch = 19, col = 'red')

But when I do this in that way (I would like to have dates on the X axis, so I use start, end and frequency arguments.), points don't appear on the plot:

data <- ts(rnorm(100), start = c(1996,1), end = c(2004,4), frequency = 12)
plot(data)
ind <- c(1,10,67)
points(ind, data[ind], pch = 19, col = 'red')

Is there any option to add those points or add dates to the first example?

PS. I am almost sure, that I have once managed to add one point to plot by referring to the value of time series in specific date, but now I can't bring it back.

like image 213
Sebastian Osiński Avatar asked May 18 '14 13:05

Sebastian Osiński


People also ask

What does TS function do in R?

The ts() function will convert a numeric vector into an R time series object. The format is ts(vector, start=, end=, frequency=) where start and end are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).

What does plot TS mean in R?

R Documentation. plot.ts: Plotting Time-Series Objects.


2 Answers

This works for me:

data <- ts(rnorm(100), start = c(1996,1), end = c(2004,4), frequency = 12)
plot(data)
ind <- 1996 + c(0,9,66)/12
points(ind, data[c(1,10,67)], pch = 19, col = 'red')
like image 200
talat Avatar answered Oct 23 '22 14:10

talat


I would rather use the zoo package:

data <- as.zoo(data)
plot(data)
ind <- c(1,10,67)
points(data[ind], pch = 19, col = 'red')

That way you can play with the index without having to worry about the date (it seemed to me that is what you wanted).

like image 29
Peque Avatar answered Oct 23 '22 14:10

Peque