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? Thanks! Charles /****************************************** ** Charles H. Franklin ** Professor, Political Science ** University of Wisconsin, Madison ** 1050 Bascom Mall ** Madison, WI 53706 ** 608-263-2022 Office ** 608-265-2663 Fax ** mailto:franklin at polisci.wisc.edu (best) ** mailto:chfrankl at facstaff.wisc.edu (alt) ** http://www.polisci.wisc.edu/~franklin ******************************************/
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
One thing you could do is to use the 'its' (irregular time-series) package on CRAN. e.g. using a trivial dataset require(its) its.format("%Y-%m-%d") #defines text format of dates in dimnames df <- data.frame(1:3,(1:3)^2) dimnames(df) <- list(c("2003-01-03","2003-01-06","2003-01-07"),letters[1:2]) plot(its(as.matrix(df)),type="p") or more simply mat <- structure(1:6,dim=c(3,2),dimnames=list(c("2003-01-03","2003-01-06","2003-01- 07"),letters[1:2])) plot(its(mat),type="p") Clearly plot does not provide the same functionality as the beautifully crafted xyplot of lattice, but dates do get handled as an abcissa. Incidentally the implementation of 'its' uses the POSIXct class, but this is largely encapsulated. Giles> -----Original Message----- > From: Charles H. Franklin [mailto:franklin at polisci.wisc.edu] > Sent: 17 September 2003 04:00 > To: r-help at stat.math.ethz.ch > Subject: [R] Date on x-axis of xyplot > > > 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? > > Thanks! > > Charles > > > > /****************************************** > ** Charles H. Franklin > ** Professor, Political Science > ** University of Wisconsin, Madison > ** 1050 Bascom Mall > ** Madison, WI 53706 > ** 608-263-2022 Office > ** 608-265-2663 Fax > ** mailto:franklin at polisci.wisc.edu (best) > ** mailto:chfrankl at facstaff.wisc.edu (alt) > ** http://www.polisci.wisc.edu/~franklin > ******************************************/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >********************************************************************** This is a commercial communication from Commerzbank AG.\ \ T...{{dropped}}