[R] Ggplot and irregular timeseries

Alex Bird sunduck at gmail.com
Fri Nov 19 12:28:01 CET 2010


Hello there,

  Could anybody please help on how to correctly use ggplot when
printing irregural time series, by irregural here I mean for example
the absence of some dates in the whole timespan of a dataset. To be
more precise in the following example I generated some random data
which spans the whole November up to now and dropped weekend days but
for some reasons ggplot continues to plot the days I just dropped. The
ggplot generated outputs for comparison purposes can be found here
(http://img269.imageshack.us/img269/121/ggplotweekends.png) where the
upper image is for the regular timeseries and the bottom one is for
the irregular one. Datasets for both the charts are
(http://img521.imageshack.us/img521/6729/regularts.png) and
(http://img517.imageshack.us/img517/8651/irregularts.png)
correspondingly.  The R code used to generate them is

dates<-seq(ISOdate(2010,11,1), by = "day", length.out = 19)
rets<-xts(rlnorm(19),order.by=dates)
# the following line drops weekends from the timeseries
rets<-rets[!weekdays(index(rets))%in%c('Saturday','Sunday'),]

rets.m<-data.frame(index(rets),rets,check.names=F)
colnames(rets.m)<-c('Date','Rets')
rets.m<-melt(rets.m,id.vars=c('Date','Rets'))
ggplot(data=rets.m,aes(Date,Rets))+geom_bar(stat="identity")+scale_x_datetime(major
= freqprint, format='%m/%d')

Thanks in advance for your help/patience and my bad English.

Kind regards,
Alex



More information about the R-help mailing list