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.
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.).
R Documentation. plot.ts: Plotting Time-Series Objects.
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')
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With