[R] Date on x-axis of xyplot

Deepayan Sarkar deepayan at stat.wisc.edu
Wed Sep 17 06:31:06 CEST 2003


On Tuesday 16 September 2003 22:00, Charles H. Franklin wrote:
> xyplot doesn't seem to want to label my x-axis with dates but instead puts
> the day-number for each date.
>
> begdate is the number of days since January 1, 1960 and was initially
> created by
>
> library(date)
>
> ...
>
> polls$begdate<-mdy.date(begmm,begdd,begyy)
>
> I create a new dataframe (pollstack) which includes begdate. In the process
>
> begdate seems to lose its date attribute so I redo it as:
> > pollstack$begdate<-as.date(pollstack$begdate)
>
> after which
>
> > attach(pollstack)
> > summary(pollstack)
>
>    begdate               pct              names
>  First :15Nov2002   Min.   : 0.000   Clark   : 54
>  Last  :10Sep2003   1st Qu.: 2.000   Dean    : 54
>                     Median : 5.000   Edwards : 54
>                     Mean   : 6.991   Gephardt: 54
>                     3rd Qu.:12.000   Graham  : 54
>                     Max.   :29.000   Kerry   : 54
>                                      (Other) :216
>
>
> And all seems well.
>
> But xyplot continues to use day number on the x-axis. My plots are created
> by
>
>  print(xyplot(pct ~ begdate | names, pch=2, cex=.2,
>    prepanel = function(x, y) prepanel.loess(x, y, span = 1),
>    main="2004 Democratic Primary Race",
>    xlab = "Date of Survey",
>    ylab = "Percent Support",
>    panel = function(x, y) {
>        panel.grid(h=-1, v= -1)
>        panel.xyplot(x, y, pch=1,col=2,cex=.7)
>        panel.loess(x,y, span=.65, lwd=2,col=4)
>       }, ) )
>
> What am I missing?

The fact that xyplot doesn't know anything about the "date" class. I'm not 
familiar with the date package, but the docs and a few experiments seem to 
indicate that an object of class "date" is simply a numeric/integer vector 
with the class attribute set to "date". xyplot interprets it as plain numeric 
data.

You may be able to get what you want by 

print(xyplot(pct ~ factor(as.character(begdate)) | names, pch=2, cex=.2,
      prepanel = function(x, y) prepanel.loess(x, y, span = 1),
      ...


(but this will try to label all unique dates, which may not be good).


Is the date class standard enough to warrant including a check for it in 
lattice ?

Deepayan




More information about the R-help mailing list