[R] How to add separate labels to the axes of each panel plot?

Marius Hofert m_hofert at web.de
Fri May 7 12:59:23 CEST 2010


Dear R experts,

I have four panels plotted with relation="free" and I would like to have labels for both axis for each of the four plots. 
How can this be done? Below is a minimal example. There are two problems:
1) How to determine the current panel being plotted inside the panel function.
2) How to choose the location of the label without having to worry about the x- and y-axis units.

Cheers,

Marius

## panel function
library(lattice)
x=c(1,2,1,2,1,2,1,2)
y=c(1,2,2,1,1,-1,-2000,2000)
z=c(1,1,2,2,3,3,4,4) # panel number
xyplot(y~x|z,type="l",aspect=1,layout=c(2,2),
       scales=list(y=list(rot=0),relation="free",alternating=c(1,1),tck=c(1,0)),
       par.settings = list(clip=list(panel=FALSE),
       layout.widths = list(axis.panel = 2),
       layout.heights = list(axis.panel = 2)),
       xlab="",ylab="",
       panel=function(...){
               panel.xyplot(...,col=1)
               panel.text(x=unit(1.5,"npc"),y=unit(-1.7,"npc"),"x axis")
       }    
)


More information about the R-help mailing list