Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a certain range of x values of dates and times? Error "invalid xlim value"

Tags:

plot

r

I'm having a problem with plotting time series data if I want to zoom in a certain time frame within the entire x domain (time axis). See the following code for context:

date_time <- as.factor(c('8/24/07 17:30','8/24/07 18:00','8/24/07 18:30',
                     '8/24/07 19:00','8/24/07 19:30','8/24/07 20:00',
                     '8/24/07 20:30','8/24/07 21:00','8/24/07 21:30',
                     '8/24/07 22:00','8/24/07 22:30','8/24/07 23:00',
                     '8/24/07 23:30','8/25/07 00:00','8/25/07 00:30'))

U. <- as.factor(c('0.2355','0.2602','0.2039','0.2571','0.1419','0.0778','0.3557',
              '0.3065','0.1559','0.0943','0.1519','0.1498','0.1574','0.1929','0.1407'))

dts<-strptime(as.character(date_time),'%m/%d/%y %H:%M')

plot(dts,as.numeric(U.),xlim=c('2007-08-24 17:30:00','2007-08-24 19:00:00'))

My question: Why doesn't xlim work in this case and how to fix it? Please help.

Note: ignore as.factor and as.numeric, I added these to mimic the actual data once I import it into R

like image 855
Yusri Avatar asked May 29 '14 06:05

Yusri


1 Answers

You need to tell R that your xlim values are dates and times. So try using

xlim=c(as.POSIXct('2007-08-24 17:30:00', format="%Y-%m-%d %H:%M:%S"),
                         as.POSIXct('2007-08-24 19:00:00', format="%Y-%m-%d %H:%M:%S"))

And on a side note, as.numeric(U.) will probably not result in what you actually want. You should use as.numeric(as.character(U.)) instead - compare the results.

like image 159
talat Avatar answered Oct 14 '22 04:10

talat