[R] lattice scales format dates

Folkes, Michael Michael.Folkes at dfo-mpo.gc.ca
Wed Aug 6 23:45:14 CEST 2014

Thanks David.
Much appreciated.

-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: August-06-14 2:31 PM
To: Folkes, Michael
Cc: r-help at r-project.org
Subject: Re: [R] lattice scales format dates

On Aug 6, 2014, at 1:11 PM, Folkes, Michael wrote:

> Hello all,
> Based on the help in ?xyplot,

Which says very little about barchart in particular but rather refers
you to 


It's really designed expecting  x to be a factor,

> and this suggestion from another request:
> http://stackoverflow.com/questions/20623041/r-formatted-auto-scaling-d
> at
> e-axis-using-lattice
> I was under the impression that this code should give me the desired x

> axis label date format of: "1911-Jan".

Probably would have if you were using xyplot. But barchart's author
assumed that the "x-axis" is a set of disjoint values and that's not
really how datetimes are generally considered. Try this:

 barchart(var1~factor(format(date.val, "%Y-%b")), data=dat,
          scales=list(x=list(rot=45) ) )

> dat  <- data.frame(var1=1:10,
> date.val=as.POSIXct(seq(as.Date("1910/11/1"), as.Date("1911/8/1"),
> "months") ))
> barchart(var1~date.val, data=dat, horizontal=F, 
> scales=list(x=list(rot=45, format="%Y-%b")))
> format(dat$date.val,"%Y-%b")
And learn to post in plain text.
> 	[[alternative HTML version deleted]]

David Winsemius
Alameda, CA, USA

More information about the R-help mailing list