1

I have an xts series that I'm trying to plot. This series contains of intra-day date for a month with gaps in the data on the weekend. I use xyplot (lattice) in R to plot the time series and am very pleased with the results.

Unfortunately the plots keep the weekend gaps. I'd like to ignore the weekend gaps and make my timeseries plot continuous and would appreciate if someone pointed me in the right direction.

The current command is :

xyplot(close~MyTime, type='l', col='black',ylab='',xlab='', main='Test')

enter image description here

I tried JohnPaul's method and it 'nearly' works. The labels while present, don't render correctly. The last label only goes up to the 3rd of January, while the actual data extends all the way up to February. The command used was:

PlotOrd<-order(Mytime)
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Close',scales=list( x=list( labels=MyTime))  )

enter image description here

user1480926
  • 634
  • 5
  • 12

1 Answers1

3

If I understand this correctly, what you wish to do is have the weekends not appear in the plot at all. One way to do this is to make another vector which is the order in which you want close plotted - a vector that does not include weekends. Assuming that weekends are not included in time this should work:

PlotOrd<-order(Mytime)

xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test')

This will give you the correct plot, but your x axis tick labels will just be the number from PlotOrd. If you want to keep them as dates add a scales argument for the x axis, like so:

xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test',
scales=list( x=list( labels=Mytime )) )

EDIT

One way to control the axis labels is to use the at argument as well. It is kind of clunky here and I wish I could come up with a more elegant idea, but this will work:

xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test',
scales=list( x=list(at=c(50,100,150,200), labels=Mytime[c(50,100,150,200)] )) )

This will put ticks at observations 50,100,150 and 200, and will give corresponding values from MyTime as the label. The downside is you have to write in the ticks yourself. You could add some code to make a sequence of values. Say you want a label every 15 days. If you figure out how many measurements that corresponds to, you can make a sequence of numbers that far apart (call it MyTicks). Then feed to at=MyTicks and labels=MyTime[MyTicks]. Still it would be nicer to have lattice just pick the ticks for you...

John Paul
  • 12,196
  • 6
  • 55
  • 75
  • Hi John Paul thank you for the response. What is PlotOrdxyp ? I assume its the same as PlotOrd. Indeed when I change the index to integers I can get them in one continuous plot, however I lose the dates. When I try the second method suggested by you, I get an error : Error in ans$labels[ok] : object of type 'closure' is not subsettable – user1480926 Apr 14 '14 at 16:58
  • @user1480926 - It should be PlotOrd - sorry about the typo I fixed it above. My guess on the second error is that `time` is the name of a function in R and so it is confused and is can't fine your data called "time". Try renaming or putting your "time" into a new vector that has a unique name and that should work. I edited the answer above using `MyTime` as the new vector. – John Paul Apr 14 '14 at 17:11
  • hi yes this works thank you very much !!! Thank you for explaining this so clearly – user1480926 Apr 14 '14 at 17:45
  • Hi John Paul the times dont quite work. See the attached image above to see what happens when I do what you suggested – user1480926 Apr 14 '14 at 17:52
  • Note that the time horizon of the data is five minutely, and the graph extends to the entire month. The original rendering of time takes this into account and seems to plot the dates correctly, but the scale command doesn't seem to like it. – user1480926 Apr 14 '14 at 17:58
  • Oops - the scales command is putting the first value of `MyTime` on the first x axis tic, and the second value on the second tic etc. Need to fix that. – John Paul Apr 14 '14 at 18:01
  • right so how do I know the number of visible tics so that I can adjust for it ? – user1480926 Apr 14 '14 at 18:53
  • @user1480926 I am trying to think of a better way, but a simple one is to use the `at` argument in scales. You give at a bunch of numbers e.g. at=c(10,20,30) it will put the ticks there. This would be the 10th, 20th, 30th value, Then tell the labels to put the correct labels by giving the same values to labels - I will edit above. – John Paul Apr 14 '14 at 19:02
  • I think this is probably the closest I'm going to get to get full control. Thank you once more John Paul – user1480926 Apr 15 '14 at 10:19