I'm trying to produce a barchart plot with groups, in which each group is placed in a particular time scale in x-axis. If I use barchart directly it does not take the time scale. I've tried with xyplot and adding a panel.barchart, I have the bars in the right place, but not the three groups I'm trying to produce. I've tried defining panel and panel.group, but can't get it to work Drate is a numeric variable with three categories, perc a percentage of preys digested. Script with barchart: barchart(Perc~as.POSIXct(hora,format="%d-%m-%Y %H:%M"),digrate,groups=Drate,horizontal=F, key = leg, 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.barchart(x,y,col=c("white","grey","black"),...)} ) 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
RICHARD M. HEIBERGER
2010-Feb-05 04:29 UTC
[R] lattice barchart using a time scale in x axis
> 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,...)} > ? ? ?) > > FranYou 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" )
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 > >