Hi list, I am unsuccessfully trying to produce a serious of trellis barcharts from within a for-loop. The barcharts work outside the loop. What am I missing? Example attached. Thanks Herry #XXXXXXXXXXXXXXXXXXXXXX trellis.device(bg="white") trellis.par.get("fontsize")->fontsize fontsize$default<-16 trellis.par.set("fontsize",fontsize) a<-c(1,2,4,5,4,3,3,3) b<-c(2,5,1,1,1,3,3,3) c<-c(3,5,1,2,2,5,5,5) as.data.frame(cbind(a,b,c))->q21 varnames<-colnames(q21) i<-9999 for (i in 1:3) { round(table(q21[,i])/sum(table(q21[,i])),3)*100->z1 as.data.frame.table(table(q21[,i]))->z1 round(z1[,2]/sum(z1[,2]),3)*100->z1[,2] colnames(z1)<-c(varnames[i],"y") xnames<-levels(z1[,1]) paste(xnames[1]," - low",sep="")->xnames[1] paste(xnames[length(xnames)]," - high",sep="")->xnames[length(xnames)] z1[,1]<-xnames as.factor(z1[,1])->z1[,1] barchart( horizontal=FALSE, z1[,2]~z1[,1], ylab=list("% frequency",cex=1.5), main=list(varnames[i],cex=1.25), scales=list(1,cex=1.5), col="#ffd18f", sub=list("Rating",cex=1.25) ) filename<-paste("test",i,"_q21k.jpg",sep="") #dev.print(jpeg, filename , width=1000, height=1000, quality=100, bg="white", pointsize=20) } [[alternate HTML version deleted]]
Trellis graphics need to be print()-ed for them to show on the device. I think if you wrap your call to barchart() with print(), it should work as you expect. -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Fri, 4 Apr 2003, Alexander.Herr at csiro.au wrote:> Hi list, > > I am unsuccessfully trying to produce a serious of trellis barcharts from > within a for-loop. The barcharts work outside the loop. What am I missing? > Example attached. > > Thanks Herry > > #XXXXXXXXXXXXXXXXXXXXXX > > > trellis.device(bg="white") > trellis.par.get("fontsize")->fontsize > fontsize$default<-16 > trellis.par.set("fontsize",fontsize) > > a<-c(1,2,4,5,4,3,3,3) > b<-c(2,5,1,1,1,3,3,3) > c<-c(3,5,1,2,2,5,5,5) > as.data.frame(cbind(a,b,c))->q21 > varnames<-colnames(q21) > i<-9999 > for (i in 1:3) { > round(table(q21[,i])/sum(table(q21[,i])),3)*100->z1 > as.data.frame.table(table(q21[,i]))->z1 > round(z1[,2]/sum(z1[,2]),3)*100->z1[,2] > colnames(z1)<-c(varnames[i],"y") > xnames<-levels(z1[,1]) > paste(xnames[1]," - low",sep="")->xnames[1] > paste(xnames[length(xnames)]," - high",sep="")->xnames[length(xnames)] > z1[,1]<-xnames > as.factor(z1[,1])->z1[,1] > barchart( > horizontal=FALSE, > z1[,2]~z1[,1], > ylab=list("% frequency",cex=1.5), > main=list(varnames[i],cex=1.25), > scales=list(1,cex=1.5), col="#ffd18f", > sub=list("Rating",cex=1.25) > ) > filename<-paste("test",i,"_q21k.jpg",sep="") > #dev.print(jpeg, filename , width=1000, height=1000, quality=100, > bg="white", pointsize=20) > } > > > [[alternate HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
On Friday 04 April 2003 12:04 am, Alexander.Herr at csiro.au wrote:> Hi list, > > I am unsuccessfully trying to produce a serious of trellis barcharts from > within a for-loop. The barcharts work outside the loop. What am I missing?An explicit print() outside the barchart call. The result of barchart is a "trellis" object, which needs to be printed for anything to be plotted. Deepayan
HI, try to use 'print.trellis()' Martina ----------------------------- see: library(lattice) trellis.device(bg="white") trellis.par.get("fontsize")->fontsize fontsize$default<-16 trellis.par.set("fontsize",fontsize) a<-c(1,2,4,5,4,3,3,3) b<-c(2,5,1,1,1,3,3,3) c<-c(3,5,1,2,2,5,5,5) as.data.frame(cbind(a,b,c))->q21 varnames<-colnames(q21) i<-9999 for (i in 1:3) { round(table(q21[,i])/sum(table(q21[,i])),3)*100->z1 as.data.frame.table(table(q21[,i]))->z1 round(z1[,2]/sum(z1[,2]),3)*100->z1[,2] colnames(z1)<-c(varnames[i],"y") xnames<-levels(z1[,1]) paste(xnames[1]," - low",sep="")->xnames[1] paste(xnames[length(xnames)]," - high",sep="")->xnames[length(xnames)] z1[,1]<-xnames as.factor(z1[,1])->z1[,1] print.trellis(barchart( horizontal=FALSE, z1[,2]~z1[,1], ylab=list("% frequency",cex=1.5), main=list(varnames[i],cex=1.25), scales=list(1,cex=1.5), col="#ffd18f", sub=list("Rating",cex=1.25) )) filename<-paste("test",i,"_q21k.jpg",sep="") #dev.print(jpeg, filename , width=1000, height=1000, quality=100, bg="white", pointsize=20) } -------------------------------------------------------------------------- Department of Statistics Office Phone: (614) 292-1567 1958 Neil Avenue, 304E Cockins Hall FAX: (614) 292-2096 The Ohio State University E-mail: pavlicov at stat.ohio-state.edu Columbus, OH 43210-1247 www.stat.ohio-state.edu/~pavlicov On Fri, 4 Apr 2003 Alexander.Herr at csiro.au wrote:> Hi list, > > I am unsuccessfully trying to produce a serious of trellis barcharts from > within a for-loop. The barcharts work outside the loop. What am I missing? > Example attached. > > Thanks Herry > > #XXXXXXXXXXXXXXXXXXXXXX > > > trellis.device(bg="white") > trellis.par.get("fontsize")->fontsize > fontsize$default<-16 > trellis.par.set("fontsize",fontsize) > > a<-c(1,2,4,5,4,3,3,3) > b<-c(2,5,1,1,1,3,3,3) > c<-c(3,5,1,2,2,5,5,5) > as.data.frame(cbind(a,b,c))->q21 > varnames<-colnames(q21) > i<-9999 > for (i in 1:3) { > round(table(q21[,i])/sum(table(q21[,i])),3)*100->z1 > as.data.frame.table(table(q21[,i]))->z1 > round(z1[,2]/sum(z1[,2]),3)*100->z1[,2] > colnames(z1)<-c(varnames[i],"y") > xnames<-levels(z1[,1]) > paste(xnames[1]," - low",sep="")->xnames[1] > paste(xnames[length(xnames)]," - high",sep="")->xnames[length(xnames)] > z1[,1]<-xnames > as.factor(z1[,1])->z1[,1] > barchart( > horizontal=FALSE, > z1[,2]~z1[,1], > ylab=list("% frequency",cex=1.5), > main=list(varnames[i],cex=1.25), > scales=list(1,cex=1.5), col="#ffd18f", > sub=list("Rating",cex=1.25) > ) > filename<-paste("test",i,"_q21k.jpg",sep="") > #dev.print(jpeg, filename , width=1000, height=1000, quality=100, > bg="white", pointsize=20) > } > > > [[alternate HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >