0
votes

I am having issues with trying to plot some Time Series data; namely, trying to plot the date (increments in months) against a real number (which represents price).

I can plot the data with just plot(months, mydata) with no issue, but its in a scatter plot format.

However, when I try the same with ts.plot i.e. tsplot(months, mydata), I get the following error:

Error in .cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = TRUE) : no time series supplied

I tried to bypass this by doing tsplot(ts(months, mydata)), but with this I get a straight linear line (which I know isn't correct).

I have made sure that both months and mydata have the same length

EDIT: What I mean by custom x-axis

I need the data to be in monthly increments (specifically from 03/1998 to 02/2018) - so I ran the following in R:

d <- seq(as.Date("1998-03-01"), as.Date("2018-02-01"), "day")
months <- seq(min(d), max(d), "month")

Now that I have attained the monthly increments, I need the above variable, months, to act as the x-axis for the Time Series plot (perhaps more accurately, the time index).

1
In the question title you say custom x-axis. Can you explain this a bit better?Rui Barradas
@RuiBarradas Hi, of course - I have updated the question to clarify what I mean by custom x-axisNaji

1 Answers

1
votes

With package zoo you can do the following.

library(zoo)

z <- zoo(mydata, order.by = months)

labs <- seq(min(index(z)), max(index(z)), length.out = 10)
plot(z, xaxt = "n")
axis(1, at = labs, labels = format(labs, "%m/%Y"))

Data creation code.

set.seed(1234)

d <- seq(as.Date("1998-03-01"), as.Date("2018-02-01"), "day")
months <- seq(min(d), max(d), "month")

n <- length(months)
mydata <- cumsum(rnorm(n))