[R] mtext in trellis-loop & colorkey

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Mon May 29 13:57:18 CEST 2006



Marius Hofert wrote:
> Hello,
> 
> I would like to create a sequence of plots (using a for loop). I read  
> in the FAQ that print() has to be used in order to obtain any output.  
> This works perfectly fine as long as I only consider one function  
> call in the loop, but I would like to add mtext() to the each plot in  
> the loop. Unfortunately, this did not work. Any suggestions?
> 
> As you can see from the provided example, there is another problem  
> with such animations: You do not see any difference in the plots  
> (because only the "height" changes). Is there any possibility to keep  
> a fixed scale for the colorkey (fixed labels and also fixed colors)  
> and to see the different heights of the function from the colors  
> (e.g. the first plot should be mainly gray (as it is the "lowest"),  
> the last one mainly white (as it is the "highest")).
> 
> Thanks very much!
> 
> marius
> 
> Here is a complete minimal example:
> 
> remove(list=objects())
> library(lattice)
> for(i in 1:4){
> 	output_file_path<-paste("~/Desktop/test_",i,".png",sep="")
> 	x<-rep(seq(-3,3,length=50),50)
> 	y<-rep(seq(-3,3,length=50),each=50)
> 	z<-x*y+10*i
> 	trellis.device(png,color=F,file=output_file_path)
> 	print(wireframe 
> (z~x*y,drape=T,distance=0,zoom=0.84,cuts=100,col.regions=gray 
> (100:400/400),colorkey=list(tick.number=6)))
> 	#print(mtext(paste("Parameter= ",1,sep=""),side=3,line=0)) #This  
> does not work!
> 	dev.off()
> }
> 

I think the "page" argument will help you the title (or simply use 
"main"; see ?xyplot). For the colorkey, set the "zlim" and "at" arguments.

library(lattice)
for(i in 1:4) {
   output_file_path <- paste("./test_", i, ".png", sep = "")
   x <- rep(seq(-3, 3, length = 50), 50)
   y <- rep(seq(-3, 3, length = 50), each = 50)
   z <- x * y + 10 * i
   trellis.device(png, color = FALSE, file = output_file_path)
   w <- wireframe(z ~ x * y, drape = TRUE,
                  distance = 0, zoom = 0.84,
                  cuts = 100, col.regions = gray(100:400/400),
                  page = function(n) {
                    label <- paste("Parameter = ", i, sep = "")
                    ltext(0.5, 1, label)
                  },
                  zlim = c(0, 50),
                  at = seq(0, 50, length = 10),
                  colorkey = list(tick.number = 6))
   print(w)
   dev.off()
}

HTH,

--sundar



More information about the R-help mailing list