5
votes

I am trying to expand the X-axis of my time series plot to be prepared for adding new data into the plot later on. However, whatever I try I get the Error in plot.window(...) : invalid 'xlim' value error.

Here is a minimal code snippet:

Data construction:

time_series <- xts(rnorm(100),seq(as.POSIXct("2012-01-01 00:00:00"), as.POSIXct("2012-01-05 03:00:00"), by="hour"))

Plotting:

plot(time_series, type='l');

The result is, as expected, a nice time series plot.

Now, I wanted to expand the x-axis and I tried:

xlim <- seq(as.POSIXct("2012-01-01 00:00:00"), as.POSIXct("2012-01-06 03:00:00"), by="hour")
plot <- (time_series, xlim = xlim, type='l')

but this is not working but results in Error in plot.window(...) : invalid 'xlim' value.

Trying the following results in the same error:

xlim <- c(as.POSIXct("2012-01-01 00:00:00"), as.POSIXct("2012-01-05 00:00:00"))

From the documentation I know that xlim has to be numeric and can be set like xlim = c(0,100) but how does it work when using xts data?

Edit: I know that this question is similar to the question Time series plot range. However, as I don't know the data that needs to be plotted in the future I am interested in this particular solution.

2
I'm not familiar with plot.xts, but xlim in base graphics is normally a vector with exactly two values (min and max) and not a sequence.Roland
I should have mentioned that I also tried a vector including mix max POSIXct values. Sorry, I will add this to my question.user1356695
In general, I suggest using plot.zoo or xtsExtra::plot.xtsGSee

2 Answers

6
votes

Like this:

plot(time_series, type='l',
     xlim=as.POSIXct(c("2012-01-01 00:00:00","2012-01-06 03:00:00")))
0
votes

Or you make a subset in xts before: df2.xts<-df.xts["2012-01-01/2012-02-01"] and Then plot(df2.xts).