[R] lattice barchart using a time scale in x axis

RICHARD M. HEIBERGER rmh at temple.edu
Fri Feb 5 05:29:38 CET 2010


> SCript with xyplot:
> xyplot(Perc~as.POSIXct(hora,format="%d-%m-%Y
> %H:%M"),digrate,groups=digrate$Drate,key=leg,xlab="time of the day",
>
> scales=list(alternating=F,tck=c(1,0),x=list(at=seq(r[1],r[2],by="hour"),labels=format(seq(r[1],r[2],"hours"),format="%H:%M"))),
>            panel=function(x,y,groups,...) {
>        panel.fill(col="white")
>        panel.barchart(x,y,groups,horizontal=F,box.ratio=1000,stack=F,...)}
>      )
>
> Fran

You do need to use xyplot() to get control of the x axis.  In this
example, the problem
comes from not specifying the arguments names to panel.barchart.  In
?panel.barchart,
the third argument is box.ratio.  The value of your groups was
therefore used as the box.ratio
and the widths of the boxes differed by group.  Here is a complete
working example, with fake
data and without r and leg.

digrate <- data.frame(Perc=runif(30), Drate=rep(letters[1:3], 10),
                  hora=rep(c(1,2,3,6,8,10,15,20,24,30),each=3))
digrate

xyplot(Perc~hora, ## as.POSIXct(hora,format="%d-%m-%Y %H:%M"),
       data=digrate, groups=digrate$Drate, ## key=leg,
       xlab="time of the day",
       scales=list(alternating=FALSE,tck=c(1,0) ##,
         ## x=list(
         ##   at=seq(r[1],r[2],by="hour"),
         ##   labels=format(seq(r[1],r[2],"hours"), format="%H:%M"))
         ),
       panel=function(x,y,groups,...) {
         panel.fill(col="white")
         panel.barchart(x, y, groups=groups, horizontal=FALSE,
                        box.ratio=5, stack=FALSE, ...)
       },
       main="xyplot"
       )



More information about the R-help mailing list