[R] How to put text outside an xyplot?

Paul Murrell p.murrell at auckland.ac.nz
Mon Dec 21 21:00:56 CET 2009


Hi


Marius Hofert wrote:
> Dear R-users,
> 
> I have a plot created with the code below. I tried to put some text
> to the right of the color key. I worked with grid.text and also
> viewport(), but couldn't achieve this. I know this should be simple,
> but I just couldn't figure out how to do it. How does this work?


The output in the panel is clipped to the panel.  You could get around 
that by pushing a viewport that turns the clipping off ...

             pushViewport(viewport(clip="off"))
             grid.text("text to the right of the color key",
                       1.2,0.5,rot=90)
             popViewport()

... (though that way you lose access to the axis coordinates in the 
panel) or, because lattice generates a version of the panel viewport 
with clipping already turned off, draw the plot first then revisit the 
panel viewport with the clipping off ...

xyplot(data[,2]~data[,1],aspect=1,
	panel=function(...){
		panel.xyplot(...,type="n")
		lpoints(data[,1],data[,2],pch=1,col=colorvec)
	},
	xlab="x",ylab="y",
legend=list(right=list(fun=draw.colorkey,args=list(key=list(col=mygray(10),at=1:10))))
)

trellis.focus("panel", 1, 1, clip.off=TRUE)
grid.text("text to the right of the color key",
           1.2, 0.5, rot=90)
trellis.unfocus()

... either way, there is still a possibility you won't see anything if 
that x=1.2 for the text is off the right-hand side of the page, so you 
might want to use a different coordinate system, or just draw the text 
relative to the right-hand side of the page rather than relative to the 
panel ...

xyplot(data[,2]~data[,1],aspect=1,
	panel=function(...){
		panel.xyplot(...,type="n")
		lpoints(data[,1],data[,2],pch=1,col=colorvec)
	},
	xlab="x",ylab="y", 
legend=list(right=list(fun=draw.colorkey,args=list(key=list(col=mygray(10),at=1:10))))
)

grid.text("text to the right of the color key",
           x=unit(1, "npc") - unit(1, "mm"),
           just="bottom", rot=90)


Hope that helps.

Paul

	
> Cheers,
> 
> Marius
> 
> library(lattice) library(grid) myvec=1:10 
> data=data.frame(x=myvec,y=myvec) mygray=function(l)
> gray(seq(0.2,0.8,length=l)) 
> colors=level.colors(data[1:10,2],at=do.breaks(c(1,10),10),col.regions=mygray)
>  colorvec=colors[1:10] 
> #trellis.device(postscript,color=F,horizontal=F,onefile=F) 
> xyplot(data[,2]~data[,1],aspect=1, panel=function(...){ 
> panel.xyplot(...,type="n") 
> lpoints(data[,1],data[,2],pch=1,col=colorvec) grid.text("text to the
> right of the color key",1.2,0.5,rot=90)#does not work. }, 
> xlab="x",ylab="y", 
> legend=list(right=list(fun=draw.colorkey,args=list(key=list(col=mygray(10),at=1:10))))
>  ) #dev.off()
> 
> ______________________________________________ R-help at r-project.org
> mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do
> read the posting guide http://www.R-project.org/posting-guide.html 
> and provide commented, minimal, self-contained, reproducible code.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list