[R] lattice scales format dates

David Winsemius dwinsemius at comcast.net
Wed Aug 6 23:30:51 CEST 2014


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 

?panel.barchart

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-dat
> 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, horizontal=F,
          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