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

Fran Velasco franvgls at gmail.com
Fri Feb 5 10:25:06 CET 2010


Hi all,
Thanks for your answers, it worked, but still can't get the time scale on the x-axis, probably has 
to do with the unit in the viewport or something like that. But following your recommendation I've 
prepared some dummy data to go with the scripts. As before we have two graphs, one that has the 
error I got, and another that has the graph I want to get, but without the time scale as needed.
Cheers,
Fran
Example:

#create the dummy data
digrate <- data.frame(Perc=runif(30), Drate=rep(letters[1:3], 10),
                   date=c(rep("26-06-2010",9),rep("27-06-2010",21)),
 
hour=rep(c("18:00","20:00","23:00","03:00","05:30","08:00","10:00","14:40","17:30","19:30"),each=3))
digrate$hora<-paste(digrate$date,digrate$hour)
digrate
library(lattice)
# xyplot with panel.barchart, but does not take the groups as in example 2
xyplot(Perc~as.POSIXct(hora,format="%d-%m-%Y %H:%M",origin=strptime(digrate$hora,"%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=5000, stack=FALSE, ...)
        },
        main="xyplot"
        )

# barchart example does not get the time scale
# get the time range for the x-axes in graph 2
r<-range(strptime(digrate$hora,"%d-%m-%Y %H:%M"))
barchart(Perc~as.POSIXct(hora,format="%d-%m-%Y %H:%M"),digrate,groups=Drate,horizontal=F,
   scales=list(alternating=F,tck=c(1,0),x=list(at=1:10,labels=format(seq(r[1],r[2],"hours"),
     format="%H:%M"))),
             panel=function(x,y,...) {panel.fill(col="white")
                 panel.grid(-1,0,lty=3,col="black")
                 panel.barchart(x,y,col=c("white","grey","black"),...)}
             )


On 05/02/2010 1:56, RICHARD M. HEIBERGER wrote:
> Fran,
>
> Please send to the list some properly structured dummy data.
> dput(digrate.dummy) would be easiest for me.
>
> Rich
>
>



More information about the R-help mailing list