[R] Dates labels on axes in xyplot

Renaud Lancelot renaud.lancelot at cirad.fr
Wed Feb 9 07:08:39 CET 2005


Dirk Eddelbuettel a écrit :

> On 9 February 2005 at 14:37, Denham Robert wrote:
> | I am a bit confused about how to get the format of the labels in xyplot
> | to show as dates rather than the numeric value.
> |  
> | e.g., 
> |  
> | x <- as.Date(c("20/01/2001","20/02/2003","21/06/2004"),"%d/%m/%Y")
> | y <- c(1,2,3)
> |  
> | plot(y~x)     #produces formatted labels
> |  
> | xyplot(y~x)  #doesn't give formatted x labels
> |  
> | xyplot(y~x,scales=list(x=list(format="%d/%m/%Y"))) #still doesn't.
> |  
> | How do I get the format for the xlabels ?
> 
> You need another as.POSIXct():
> 
> 
>>x <- as.POSIXct(as.Date(c("20/01/2001","20/02/2003","21/06/2004"),"%d/%m/%Y"))
>>y <- c(1,2,3)
>>xyplot(y~x)
> 
> 
> Dirk
> 

Does not work for me (R 2.0.1, Windows, current version of lattice).

The following does:

x <- as.Date(c("20/01/2001", "20/02/2003", "21/06/2004"), "%d/%m/%Y")
y <- c(1,2,3)

library(lattice)

AT <- pretty(x)
LAB <- as.Date("1970-01-01") + AT
xyplot(y ~ x,
   scales = list(x = list(at = AT, labels = format(LAB, "%d/%m/%Y"))))

Best,

Renaud


-- 
Dr Renaud Lancelot, vétérinaire
C/0 Ambassade de France - SCAC
BP 834 Antananarivo 101 - Madagascar

e-mail: renaud.lancelot at cirad.fr
tel.:   +261 32 40 165 53 (cell)
         +261 20 22 665 36 ext. 225 (work)
         +261 20 22 494 37 (home)




More information about the R-help mailing list