5

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.

4

2 回答 2

6

像这样:

plot(time_series, type='l',
     xlim=as.POSIXct(c("2012-01-01 00:00:00","2012-01-06 03:00:00")))
于 2013-07-04T16:45:51.010 回答
0

或者你之前在 xts 中创建一个子集:df2.xts<-df.xts["2012-01-01/2012-02-01"] 然后 plot(df2.xts)。

于 2013-07-05T16:43:25.093 回答