Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R setting xlim in xts plot

Tags:

plot

r

xts

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.

like image 438
user1356695 Avatar asked Jul 04 '13 16:07

user1356695


1 Answers

Like this:

plot(time_series, type='l',
     xlim=as.POSIXct(c("2012-01-01 00:00:00","2012-01-06 03:00:00")))
like image 70
Roland Avatar answered Oct 20 '22 15:10

Roland